• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Try-catch-finally doubt

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given-



What happened to the exception thrown from the catch block? Did it get lost?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 91
Firefox Browser C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic