6

In exception handling, what will happen if catch block or finally block having Exception?

Not a bug
  • 4,286
  • 2
  • 40
  • 80
Tilak Raj
  • 472
  • 4
  • 12

2 Answers2

5

Finally block exception will mask the original exception.

When an new exception is thrown in a catch block or finally block that will propagate out of that block, then the current exception will be aborted (and forgotten) as the new exception is propagates outward.

Check here and here for more details

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
1

As per the JLS 14.20.2. Execution of try-finally and try-catch-finally

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).

A finally block may throw an exception and if so, any exception thrown by the try or catch block is lost.

Ref: http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.2

Zeeshan
  • 11,851
  • 21
  • 73
  • 98