Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of exceptions that CAN'T be caught in .NET

Tags:

What is the list of exceptions that CAN'T be caught in .NET? Or where can I find such a list?

like image 845
Denis Avatar asked Sep 12 '11 19:09

Denis


People also ask

What exceptions Cannot be stopped in the catch block?

Any exception that is thrown by the jitter before your code can start running cannot be caught or reported. Failure to compile your Main() method is the common case, typically a FileNotFoundException.

How many exceptions are there in C#?

There are two types of exceptions: exceptions generated by an executing program and exceptions generated by the common language runtime. System. Exception is the base class for all exceptions in C#. Several exception classes inherit from this class including ApplicationException and SystemException.

What happens if exception is not caught C#?

In C#, the catch keyword is used to define an exception handler. If no exception handler for a given exception is present, the program stops executing with an error message. Don't catch an exception unless you can handle it and leave the application in a known state. If you catch System.


2 Answers

The only exception that cannot be caught directly is (a framework thrown) StackOverflowException. This makes sense, logically, as you don't have the space in the stack to handle the exception at that point. From the docs:

Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default.

ThreadAbortException can be caught, but will always get re-raised, so has unique behavior. From the docs:

ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.

Also note that some AccessViolationException instances are corrupted state exceptions, and may not get handled by default. These can be handled, but require extra handling via attributes. For details, see Handling Corrupted State Exceptions.

like image 153
Reed Copsey Avatar answered Sep 20 '22 04:09

Reed Copsey


NullReferenceException can certainly be caught. Where did you get the idea from?

A try {} catch {} will catch non managed exceptions as well as managed ones (note that there is not exception clause on the catch).

The only one that cannot be caught is StackOverflowException, and TreadAbortException gets rethrown at the end of the catch.

like image 22
Oded Avatar answered Sep 20 '22 04:09

Oded