4

Possible Duplicate:
throws Exception in finally blocks

  • The catch block is only executed if an exception is thrown in the try block.

  • The finally block is executed always after the try(-catch) block, if an exception is thrown or not.

My question is IF I got Exception in finally block than how to handle it ?????

Community
  • 1
  • 1
Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 24

2 Answers2

7

This is a well-known problem/gotcha in the Java language specification, in the sense that if an exception is thrown within the finally clause (without handling it in a nested try-catch) , the original exception gets lost. You will need to nest a new try-catch to catch the new exception, and process it there.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • 3
    in java 7 there is a way to handle it. there is now a notion of "suppressed" exceptions where you can tie multiple exceptions together (see [Throwable.addSuppressed()](http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#addSuppressed%28java.lang.Throwable%29)). this was added for the new try-with-resources feature. – jtahlborn Aug 22 '12 at 15:09
  • 1
    +1 You have the same problem with throwing exceptions in the catch block. – Peter Lawrey Aug 22 '12 at 15:21
3

You have to handle Exception in finally block

like

finally{
    try
    {
       ///
    }
    catch(Exception e)
    {
      ///  
    }
}
MAC
  • 15,799
  • 8
  • 54
  • 95