What is the list of exceptions that CAN'T be caught in .NET? Or where can I find such a list?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With