• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try-catch-finally doubt

 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given-



What happened to the exception thrown from the catch block? Did it get lost?
 
Anurag Jai
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
C++ Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic