What happens if both catch and finally blocks throw exception?
A:
When catch throws an exception, finally block will be run and then exit with an exception. If the finally block throws an exception, the block will exit with an exception.
NawaMan
2009-09-26 23:26:29
so at last what exception will be thrown? from the finally block or from the catch block?
Arthur
2009-09-26 23:30:21
The last thrown exception. :D
NawaMan
2009-09-27 05:34:42
+7
A:
When the finally block throws an exception, it will effectively hide the exception thrown from the catch block and will be the one ultimately thrown. It is therefore important to either log exceptions when caught, or make sure that the finally block does not itself throw an exception, otherwise you can get exceptions being thrown that are stifled and never seen.
adrianbanks
2009-09-26 23:32:50
+1
A:
Its already been answered well by adrianbanks, but the following post should be interesting: Interesting Exception Results: Throwing Exceptions From the Finally Block
chrisb
2009-09-26 23:34:29