Pages

Wednesday, 22 July 2015

catch Clause

The catch Clause

The catch clause handles exceptions. There are three forms, allowing different levels of processing.
catch
{
 [statements]
}
General catch clause
  • Does not have a parameter list after catch keywords.
  • Matches any exception raised in try block
catch ([Exception Type])
{
 [statements]
}
Specific catch clause
  • Takes the name of an exception class as a single parameter
  • Matches any exception of the named type
catch ([Exception Type] [Exception Variable])
{
 [statements]
}
Specific catch clause with object
  • Include identifier after the name of exception class
  • The identifier act as local variable in the block of catch clause and is called as Exception Variable
  • The exception variable references the exception object, and can be used to access information about the exception 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.
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.
  • 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 for NullReferenceException.
  • If there is a general catch clause, it must be last, after all specific catch clauses.

0 comments:

Post a Comment