The catch Clause
The catch clause handles exceptions. There are three forms, allowing different levels of processing.
The general catch clause can accept any exception but can’t determine the type of exception that caused it. This allows only general processing and cleanup for whatever exception might occur.
The specific catch clause form takes the name of an exception class as a parameter. It matches exceptions of the specified class or exception classes derived from it.
The specific catch clause with object form gives you the most information about the exception. It matches exceptions of the specified class, or exception classes derived from it. It gives you a reference to the exception object created by the CLR by assigning it to the exception variable. You can access the exception variable’s properties within the block of the catch clause to get specific information about the exception raised.
For example:
When an exception is raised, the system searches the list of catch clauses in order, and the first catch clause that matches the type of the exception object is
executed. Because of this, there are two important rules in ordering the catch clauses.
catch { [statements] } |
General catch clause
|
catch ([Exception Type]) { [statements] } |
Specific catch clause
|
catch ([Exception Type] [Exception Variable]) { [statements] } |
Specific catch clause with object
|
The general catch clause can accept any exception but can’t determine the type of exception that caused it. This allows only general processing and cleanup for whatever exception might occur.
The specific catch clause form takes the name of an exception class as a parameter. It matches exceptions of the specified class or exception classes derived from it.
The specific catch clause with object form gives you the most information about the exception. It matches exceptions of the specified class, or exception classes derived from it. It gives you a reference to the exception object created by the CLR by assigning it to the exception variable. You can access the exception variable’s properties within the block of the catch clause to get specific information about the exception raised.
For example:
static void Main(string[] args) { int x = 10, y = 0; try { Console.WriteLine("First try deviding by Zero"); x /= y; } catch { Console.WriteLine("Exception Catched" ); } try { Console.WriteLine("Second try deviding by Zero"); x /= y; } catch(DivideByZeroException) { Console.WriteLine("Exception Catched"); } try { Console.WriteLine("Third try deviding by Zero"); x /= y; } catch(DivideByZeroException e) { Console.WriteLine("Exception Catched: " + e.Message); } Console.Read(); }
Using the general catch clause is discouraged because it can hide bugs by allowing the program to continue execution when your code should be handling the error
in a specific way. It can also leave the program in an unknown state. Therefore, you should use one of the specific catch clauses if at all possible.
Multiple catch Clauses
The catch clauses section can contain multiple catch clauses.- The specific catch clauses must be ordered with the most specific exception types first, progressing to the most general. For example, if you declare an
exception class derived from
NullReferenceException
, the catch clause for your derived exception type should be listed before the catch clause forNullReferenceException
. - If there is a general catch clause, it must be last, after all specific catch clauses.
0 comments:
Post a Comment