Exception Handling in .NET

You might say, “Well, why is this a complete article?” It is that important! Errors in your program will bring the whole program down, possibly destroying the entire user’s data since last save (if there is even that feature). That’s a BAD situation.

This is where Error Handling comes into play! A very essential part of programming and a very tedious part as well. A good bit of programming is error control/checking/handling.

Exception handling is an in built mechanism in .NET framework to detect and handle run time errors. The .NET framework contains lots of standard exceptions. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors. If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution. 

C# provides three keywords try, catch and finally to do exception handling. The try encloses the statements that might throw an exception whereas catch handles an exception if one exists. The finally can be used for doing any clean up process.

The general form try-catch-finally in C# is shown below 

try

{

      // Statement which can cause an exception.

}

catch(Type x)

{

      // Statements for handling the exception

}

finally

{

      //Any cleanup code

}

If any exception occurs inside the try block, the control transfers to the appropriate catch block and later to the finally block.  

But in C#, both catch and finally blocks are optional. The try block can exist either with one or more catch blocks or a finally block or with both catch and finally blocks. 

If there is no exception occurred inside the try block, the control directly transfers to finally block. We can say that the statements inside the finally block is executed always. Note that it is an error to transfer control out of a finally block by using break, continue, return or goto. 

In C#, exceptions are nothing but objects of the type Exception. The Exception is the ultimate base class for any exceptions in C#. The C# itself provides couple of standard exceptions. Or even the user can create their own exception classes, provided that this should inherit from either Exception class or one of the standard derived classes of Exception class like DivideByZeroExcpetion ot ArgumentException etc. 

Uncaught Exceptions 

The following program will compile but will show an error during execution. The division by zero is a runtime anomaly and program terminates with an error message. Any uncaught exceptions in the current context propagate to a higher context and looks for an appropriate catch block to handle it. If it can’t find any suitable catch blocks, the default mechanism of the .NET runtime will terminate the execution of the entire program. 

//C#: Exception Handling

using System;

class MyClient

{

            public static void Main()

            {

                        int x = 0;

                        int div = 100/x;

                        Console.WriteLine(div);

            }

}

The modified form of the above program with exception handling mechanism is as follows. Here we are using the object of the standard exception class DivideByZeroException to handle the exception caused by division by zero. 

//C#: Exception Handling         

using System;

class MyClient

{

       public static void Main()

       {

              int x = 0;

              int div = 0;

              try

              {

                     div = 100/x;

                     Console.WriteLine(“This line in not executed”);

              }

              catch(DivideByZeroException de)

              {

                     Console.WriteLine("Exception occured");

}

              Console.WriteLine("Result is {0}",div);

       }

}   

In the above case the program do not terminate unexpectedly. Instead the program control passes from the point where exception occurred inside the try block to the catch blocks. If it finds any suitable catch block, executes the statements inside that catch and continues with the normal execution of the program statements.

If a finally block is present, the code inside the finally block will get also be executed.

//C#: Exception Handling

using System;

class MyClient

{

       public static void Main()

       {

              int x = 0;

              int div = 0;

              try

              {

                     div = 100/x;

                     Console.WriteLine("Not executed line");

              }

              catch(DivideByZeroException de)

              {

                     Console.WriteLine("Exception occured");

              }

              finally

              {

                     Console.WriteLine("Finally Block");

              }

                     Console.WriteLine("Result is {0}",div);

       }

}

Multiple Catch Blocks 

A try block can throw multiple exceptions, which can handle by using multiple catch blocks. Remember that more specialized catch block should come before a generalized one. Otherwise the compiler will show a compilation error. 

//C#: Exception Handling: Multiple catch       

using System;

class MyClient

       {

              public static void Main()

              {

                     int x = 0;

                     int div = 0;

                     try

                     {

                                  div = 100/x;

                                  Console.WriteLine("Not executed line");

                     }

                     catch(DivideByZeroException de)

                     {

                           Console.WriteLine("DivideByZeroException" );

                     }

                     catch(Exception ee)

                     {

                           Console.WriteLine("Exception" );

                     }

                     finally

                     {

                           Console.WriteLine("Finally Block");

                     }

                           Console.WriteLine("Result is {0}",div);

                     }

}

 

Catching all Exceptions 

By providing a catch block without a brackets or arguments, we can catch all exceptions occurred inside a try block. Even we can use a catch block with an Exception type parameter to catch all exceptions happened inside the try block since in C#, all exceptions are directly or indirectly inherited from the Exception class.  

//C#: Exception Handling: Handling all exceptions

using System;

class MyClient

{

       public static void Main()

       {

              int x = 0;

              int div = 0;

              try

              {

                     div = 100/x;

                     Console.WriteLine("Not executed line");

              }

              catch

              {

                     Console.WriteLine("oException" );

              }

                     Console.WriteLine("Result is {0}",div);

       }

}

The following program handles all exception with Exception object. 

//C#: Exception Handling: Handling all exceptions

using System;

class MyClient

{

       public static void Main()

       {

              int x = 0;

              int div = 0;

              try

              {

                     div = 100/x;

                     Console.WriteLine("Not executed line");

              }

              catch(Exception e)

              {

                     Console.WriteLine("oException" );

              }

                     Console.WriteLine("Result is {0}",div);

       }

}

Design Guidelines 

Exceptions should be used to communicate exceptional conditions. Don’t use them to communicate events that are expected, such as reaching the end of a file. If there’s a good predefined exception in the System namespace that describes the exception condition-one that will make sense to the users of the class-use that one rather than defining a new exception class, and put specific information in the message. Finally, if code catches an exception that it isn’t going to handle, consider whether it should wrap that exception with additional information before re-throwing it.

===================
With Regards,
UVN PardhaSaradhi
http://spaces.msn.com/members/uvnpsaradhi/

Advertisements