Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-05-2007, 09:26 PM
Member
 
Join Date: Nov 2007
Posts: 97
Rep Power: 0
javaplus is on a distinguished road
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:

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 08:18 PM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-05-2007, 10:26 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
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:
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());
}
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-06-2007, 08:22 PM
Member
 
Join Date: Nov 2007
Posts: 97
Rep Power: 0
javaplus is on a distinguished road
Default
Thanks. So the example you gave, can have br.close() in finally block.

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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-06-2007, 08:53 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
need block letters?? dc2acgsr99 New To Java 16 01-29-2008 09:31 AM
try catch!? Joe2003 Advanced Java 2 01-28-2008 08:51 PM
Try Catch Renegade85 New To Java 4 12-03-2007 05:10 PM
when to use try...catch javaplus New To Java 2 11-18-2007 09:52 PM
Use try and catch zoe New To Java 2 07-25-2007 08:50 PM


All times are GMT +2. The time now is 09:38 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org