This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "exceptions" Watch "exceptions" New topic
Author

exceptions

Martin Smith
Greenhorn

Joined: Apr 05, 2003
Posts: 22
look at this code - It's like the exception gets "lost" - odd or what?
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

The purpose of a finally block is to execute some code regardless of how you exit the try block. That is, you could finish executing the try block normally, return from the inside the block, or throw an exception from inside the block. The finally block is great for closing an I/O stream, socket, db connection, or something similar. You've chosen to put a return statement there though. The exception stops execution of the try block, but the finally block still executes and returns true. You cannot both return a value and throw an exception from a method, so the exception "disappears".
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yup. It's almost always a bad idea to return or throw an exception from a finally clause. Note that the new JDK, j2sdk1.4.2 (beta), will give you a warning when you compile this class:
warning: finally clause cannot complete normally


"I'm not back." - Bill Harding, Twister
Martin Smith
Greenhorn

Joined: Apr 05, 2003
Posts: 22
thx!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exceptions