Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exception handling in c#

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

like image 297
NoviceToDotNet Avatar asked Oct 17 '10 14:10

NoviceToDotNet


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.

like image 51
Shaul Behr Avatar answered Nov 17 '22 03:11

Shaul Behr


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.

like image 4
Motti Avatar answered Nov 17 '22 02:11

Motti