Results 1 to 4 of 4
  1. #1
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default try...catch block

    Hi,

    I am trying to explore try catch block. I know a bit about it, but I am confused a little. How can I use multiple catch blocks with a try. And whats the use of finally in try catch block?

    I normally use try catch in following way:

    Java Code:
    try{
    ...
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }
    It serves the purpose for me but I have seen codes where developers mention a specific exception in the catch block. When to use that?

    Regards.
    Last edited by javaplus; 11-06-2007 at 07:18 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The more specific you can be the more information you can get about the trouble and thus the better equipped you can be to recover from it. Exceptions can be caught from more specific to more general. Using finally is a way to cleanup if an exception causes you to leave the execution of your code, eg, an error in reading a file causes execution to leave your try block and ends up in a catch block skipping over the br.close() statement. You could close the reader in a finally block. In pseudocode:
    Java Code:
    try {
        URL url = new URL(some_path);
        File file = new File(url.toURI());
        BufferedReader br = new BufferedReader(
                            new InputStreamReader(
                            new FileInputStream(file)));
        // read and process file
        br.close();
    } catch(MalformedURLException mue) {
        System.out.println("Bad URL: " + mue.getMessage());
    } catch(FileNotFoundException fnfe) {
        System.out.println("FileNotFound: " + fnfe.getMessage());
    } catch(IOException ioe) {
        System.out.println("Read error: " + ioe.getMessage());
    }

  3. #3
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Thanks. So the example you gave, can have br.close() in finally block.

    Java Code:
    try {
        URL url = new URL(some_path);
        File file = new File(url.toURI());
        BufferedReader br = new BufferedReader(
                            new InputStreamReader(
                            new FileInputStream(file)));
        // read and process file
    } catch(MalformedURLException mue) {
        System.out.println("Bad URL: " + mue.getMessage());
    } catch(FileNotFoundException fnfe) {
        System.out.println("FileNotFound: " + fnfe.getMessage());
    } catch(IOException ioe) {
        System.out.println("Read error: " + ioe.getMessage());
    }
    finally{
    br.close();
    }
    And what I know is, the finally block will execute even if no exception is caught. Is this right?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    the finally block will execute even if no exception is caught. Is this right?
    Yes.
    Closing the reader in the finally block may require another try-catch block for IOException.

Similar Threads

  1. need block letters??
    By dc2acgsr99 in forum New To Java
    Replies: 16
    Last Post: 01-29-2008, 08:31 AM
  2. try catch!?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-28-2008, 07:51 PM
  3. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 04:10 PM
  4. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 08:52 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •