1

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

As is documented, try/catch blocks can't handle StackOverflowException and OutOfMemoryException.

Are there any other Exceptions that also suffer from this limitation?

Community
  • 1
  • 1
Mikhail Sokolov
  • 546
  • 1
  • 7
  • 18

2 Answers2

2

Jeffrey Richter made several good points on this topic in his book CLR via C#, part "Trading Reliability for Productivity".

BTW, you can catch and handle OutOfMemmory:

For some reason that I can’t quite explain, this attention to detail is not done when writing code for the .NET Framework. Getting an out-of-memory situation is always possible and yet I almost never see any code containing a catch block to recover from an OutOfMemoryException. In fact, I’ve even had some developers tell me that the CLR doesn’t let a program catch an OutOfMemoryException. For the record, this is absolutely not true; you can catch this exception. In fact, there are many errors that are possible when executing managed code and I hardly ever see developers write code that attempts to recover from these potential failures.

Felix
  • 830
  • 10
  • 17
1

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.

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

Community
  • 1
  • 1
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
  • A bit different question i'd say: not caught, but handle... you can caught OutOfMemmory, but handling wouldn't be a smart move in general... – Felix Oct 08 '12 at 03:41