wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Try-catch-finally doubt 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 "Try-catch-finally doubt" Watch "Try-catch-finally doubt" New topic
Author

Try-catch-finally doubt

Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Given-



What happened to the exception thrown from the catch block? Did it get lost?
Anurag Jai
Greenhorn

Joined: Jun 21, 2009
Posts: 22
Hi Kedar....

As you know "finally" block is always executed, so when any exception is thrown from catch block......the exception in the finally block would be thrown from the exception instead of any exception occurring inside the catch or try block.
Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

Follow the flow control, Kedar!

You throw an exception in the try block, which is silently caught by the catch block.

The catch block then rethrows the exception. Now either it will move up the call stack (out of main()), or it will be caught again.

But in the last fragment of code, a finally block is placed. A finally block runs regardless of whether an exception is thrown. In this case the exception *is* rethrown from catch, and is 'caught' by finally, which forcefully executes. I always considered finally as an offshoot of the catch(...) clause in C++ but let's not go there, finally has some fundamental differences.

Now if we rethrow from finally, the exception is thrown out of main() and is caught by the JVM, which prints the stack trace after halting the program execution.

Now try this - don't throw a new exception from finally. Post the result.


OCJP 6
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Pritish Chakraborty wrote:Follow the flow control, Kedar!

You throw an exception in the try block, which is silently caught by the catch block.

The catch block then rethrows the exception. Now either it will move up the call stack (out of main()), or it will be caught again.

But in the last fragment of code, a finally block is placed. A finally block runs regardless of whether an exception is thrown. In this case the exception *is* rethrown from catch, and is 'caught' by finally, which forcefully executes. I always considered finally as an offshoot of the catch(...) clause in C++ but let's not go there, finally has some fundamental differences.

Now if we rethrow from finally, the exception is thrown out of main() and is caught by the JVM, which prints the stack trace after halting the program execution.

Now try this - don't throw a new exception from finally. Post the result.


Understood. That cleared my doubt!!
Thanks!
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Anurag Jai wrote:Hi Kedar....

As you know "finally" block is always executed, so when any exception is thrown from catch block......the exception in the finally block would be thrown from the exception instead of any exception occurring inside the catch or try block.

Thanks!
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

code in the finally always has the highest priority. it will execute no matter what happens in the try block(except if you call System.exit, which causes the JVM to shudown). also the code in finally block OVERRIDE any Throwable/returned value from try....catch block. so if you have something like this

try
{
return 12;
}

finally
{

return 13; // this will be returned to the calling method
}

because of the same very reason it is a bad practice to put return in finally block as it override any throwable in try block.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try-catch-finally doubt
 
Similar Threads
Exception Handling
Enthuware questions
About Exception Handling
Use of finally
wizlab mock question