*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Hi,

I am a little confused with the following code:



In the above method even though we are throwing an exception it does not have to be declared in the method signature
because the return statement is in the finally block.

However if we put the return statement outside the finally block:




this code will not compile because the InterruptedException needs to be declared in the method signature.

Could anyone please let me know why the first method compiles?
From what I understand about exception handling - if a method throws an exception then it must be declared.
I'm guessing that if the return statement is in the finally block that the int returned is taking precedence over the exception being thrown since finally block is always executed???

Many Thanks
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is from the Java Language Specification 14.20.2

If the catch block completes abruptly for reason R, then the finally block is executed. Then there is a choice:

* If the finally block completes normally, then the try statement completes abruptly for reason R.
* If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).
 
Don't get me started about those stupid light bulbs.
 
subject: Exception Handling