-3

Last week I had question, is there any exception not being caught using below catch block, please confirm..

try
{
  //code block here
}
catch(Exception ex)
{

}

Question: all exception can be caught using the Exception base class except one exception what is that exception?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Rajesh
  • 13
  • 4

2 Answers2

2

StackOverflowException is not caught because it indicates a condition that is not easily recoverable, but only in more recent versions of the .NET Framework.

http://msdn.microsoft.com/en-us/library/system.stackoverflowexception(v=vs.110).aspx

From the documentation

Version Considerations In prior versions of the .NET Framework, your application could catch a StackOverflowException object (for example, to recover from unbounded recursion). However, that practice is currently discouraged because significant additional code is required to reliably catch a stack overflow exception and continue program execution. 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. Consequently, users are advised to write their code to detect and prevent a stack overflow. For example, if your application depends on recursion, use a counter or a state condition to terminate the recursive loop. Note that an application that hosts the common language runtime (CLR) can specify that the CLR unload the application domain where the stack overflow exception occurs and let the corresponding process continue. For more information, see ICLRPolicyManager Interface and Hosting Overview.

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
0

As per the msdn

When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception. If the currently executing method does not contain such a catch block, the CLR looks at the method that called the current method, and so on up the call stack. If no catch block is found, then the CLR displays an unhandled exception message to the user and stops execution of the program.

Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61