Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-05-2007, 09:26 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 11-05-2007, 10:26 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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
javaplus is on a distinguished road
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
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 +3. The time now is 04:51 PM.


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