i want to know do finally block still execute in exception handling even if there is no matching catch block for the try block and if not then what happens? Also i want to now system exception and application difference
2 Answers
Yes, you do not need a catch
block at all. The finally
block always executes.
As to the difference between System.Exception
and System.ApplicationException
: Exception
is the base class for all exceptions; ApplicationException
should be used when a non-fatal application error occurs. See the MSDN documentation.
Also see best practices for handling exceptions.

- 36,951
- 69
- 249
- 387
-
you mean after running the finally block the exception will be thrown and the program will terminate? (when there is no matching catch block) – NoviceToDotNet Oct 17 '10 at 15:25
-
Well, yes and no. The exception will be thrown, but there could be another try-catch block surrounding the call to this one, in which case the higher level try-catch will catch this exception. – Shaul Behr Oct 17 '10 at 16:21
-
2As an aside, don't use `ApplicationException`. It is deprecated. Use an appropriate exception derived from System.Exception (or derive your own) instead. – mtreit Oct 17 '10 at 16:42
As others mentioned finally
will run even if there is no catch
block. This supports Java's try finally pattern (which can be achieved using IDisposable
and using
).
One exception (see what I did there?) is when a StackOverflowException
is thrown in which case the finally
block will not run (nor would a catch
if one were present).
The finally
block runs after the try
block completes (either cleanly or by throwing an exception) as you would expect from its location in the code.
-
P.S. I like the way the 2 Israelis answering this question are both pictured carrying a child in a backpack. (You can't see that so well in my pic, but that's what it is...) ;) – Shaul Behr Oct 17 '10 at 14:49
-
what happens it exception arise in catch block...still the finally will execute? – NoviceToDotNet Oct 17 '10 at 15:23
-
also i want to know one thing if i write try block then is it compulsory to write catch block? – NoviceToDotNet Oct 17 '10 at 15:36
-
@Novice, no it is not, you have to have _either_ a `catch` _or_ a `finally` or **both**. – Motti Oct 17 '10 at 15:46
-
@Novice, if an exception escapes the `catch` block the (outer) `finally` block is still run. – Motti Oct 17 '10 at 15:47