Reputation: 486
Is there any case where catch block is skipped when an Exception is raised say
try
{
some code
}
catch(Exception ex)
{
some code
}
I am using Exception class as it catches all the Exceptions.
Upvotes: 2
Views: 717
Reputation: 49225
As Marc has pointed out there are few exceptions that cannot be caught - for example, StackOverflowException.
With .NET 4, there is concept of Corrupted State Exceptions to indicate exceptions that are not delivered to managed code unless code has explicitly indicated an interest in handling them. These exceptions (e.g. AccessVoilationException) indicates serious programming error and in general, does not guarantee the consistent memory/program state when occurred. See this excellent article for more information: http://msdn.microsoft.com/en-us/magazine/dd419661.aspx
Upvotes: 2
Reputation: 26446
All exceptions have Exception
as base type. If the code in the try
block causes an exception, it will cause the code in the catch
block of matching type to be executed.
If you also have a more specific catch
block that matches, that block will be executed:
try
{
throw new FileNotFoundException();
}
catch (FileNotFoundException)
{
// this code will run
}
catch (Exception)
{
// this won't (but will if a different exception is thrown)
}
If the code in the catch
block causes an Exception
, it will leave the catch
block and find a "parent" catch
block that matches.
Note that only exceptions on the current thread are caught - if you start and wait for asynchronous operations within the try
block, you may not catch them on the waiting thread.
Upvotes: 2
Reputation: 4328
If there is an exception with the CLR (such as stack overflow) - it will not catch that Exception.
But for normal exceptions, you'll catch everything.
Upvotes: 1
Reputation: 12884
try
{
//some code
}
catch(System.DivideByZeroException dbz)
{
//Divide by Zero exception catched
}
catch(Exception ex)
{
//some code
}
You have given Exception
class which is the base class for all exception. So you need to give specific Exception class like ArgumentNotFound
Exception.
If you dont want to catch any Exception just leave it blank.
But keep one thing in mind, using Try..Catch
Block use high resource, so it is advisable to use it only if you are sure what kind of exception may occur.
Upvotes: 0
Reputation: 1063619
You might have problems with StackOverflowException
(see List of exceptions that CAN'T be caught in .NET)
There is another family of cases, specifically when the thing thrown wasn't an Exception
, but this is only if you are either in 1.1, or don't have automatic exception wrapping enabled (it is enabled by default from 2.0) - it is theoretically possible for C++ to throw anything (not just an Exception
), so if your "some code" calls into some C++ that throws, say, a string
, then in theory you could miss it.
In reality this is rarely (if ever) a real problem:
Exception
(or a subclass) to be well-behavedIn such cases, a catch { ... }
will work to intercept that something was thrown, but won't tell you what happened.
Upvotes: 11
Reputation: 1107
It shouldn't skip as all exceptions are derived from Exception class. Did you have any kind of occasion where it happened the other way?
Upvotes: 0