What happens if both catch and finally blocks throw exception?
-
There are different combination|nesting patterns possible with both a catch and a finally block. You might want to add a code snippet to outline the situation you are thinking about. – H H Sep 27 '09 at 16:46
-
@Justin: (Not sure if you'll be notified of this...) I rolled back your tag change because (1) you removed Java, which does dramatically change the range of the question and (2) you added VB.NET, which, while correct in many respects, is not the way things are done here... (in other words, I sort of agree with that, or removing C# as well, seeing as .NET is there). – Mark Hurd Oct 01 '12 at 06:18
-
@MarkHurd The problem is that one or more of the answers link to .Net specific solutions - I can't speak for the validity of the answers in Java. Also other VB.Net questions have links to this question (as the answer is the same). – Justin Oct 01 '12 at 08:36
-
@Justin: Yes, that's the issue with C#-tagged questions; the answers are likely to be relevant to VB.NET (and all other .NET languages), but the powers that be have deemed the questioner determines the tags, not the answers. (Although we do sometimes add tags related to the solution after an answer has been accepted, to aid searching.) In this case the Java tag means there's room for further answers or explanations. Note the current accepted answer doesn't actually refer to a specific language at all. – Mark Hurd Oct 01 '12 at 16:17
5 Answers
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.

- 81,306
- 22
- 176
- 206
-
have been tempted to do so just to make sure i control every exception message from my block code inside try. I individually throw exception – Marcel Djaman Apr 20 '15 at 11:29
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.

- 25,129
- 10
- 51
- 77
-
so at last what exception will be thrown? from the finally block or from the catch block? – Arthur Sep 26 '09 at 23:30
-
4
Its already been answered well by adrianbanks, but the following post should be interesting: Interesting Exception Results: Throwing Exceptions From the Finally Block

- 2,200
- 1
- 20
- 23
-
I've added a comment on the blog, but the Console Application result seems to be because the CLR is reporting the (first) unhandled exception during the filter phase (when VB.NET's `When` clause is processed), probably so that a debugger can be called and then locate the line of the first exception. It is most simply noted by printing something in the `finally` before the `throw`. The first exception is logged then the `finally` is processed and the second exception is logged. This occors for .NET Framework 1.1 through 3.5. .NET 4.0 only reports the first exception. (1.0 & 4.5 not available.) – Mark Hurd Oct 01 '12 at 08:18
HI Nwaman i think you answer is wrong i have tested it in windows appliaction, i found if u write a program like the below one
try
{
string s = "hu";
int i = int.Parse(s);
}
catch (Exception ex)
{
string s = "hu";
int i = int.Parse(s);
throw new Exception();
}
finally
{
MessageBox.Show("hi");
}
and this will not result finally to excute,

- 13,418
- 11
- 56
- 54