20 - Exception Handling in C#

20.1 Introduction to Exception

An exception is an error that occurs during the execution of the program. The situation arises when an operation is not completed normally. When an exception occurs in an application, the system throws an error. The error is handled through the process of exception handling.

The System.IO.IOException exception is thrown when user tries to access an incorrect stream object. If the denominator is zero, an integer division operation throws the System.DivideByZeroException exception.

Whenever an error occurs, the exception object is created by the runtime and send to the program in which the exception has occurred. This action is known as throwing an exception. The exception contains the exception object and the information about the error type.

All the exceptions are derived from the System.Execption class, which is the parent of all the classes.

Types of Errors

There are three types of errors that can occur in an application. These are:

1) Syntax errors

2) Runtime errors

3) Logical errors

1) Syntax errors

A syntax error occurs when the compiler cannot compile the code. These errors occur when the statements are not constructed properly, keywords are misspelled, or the punctuation is omitted. At the time of compilation, all these statement in the application get combined with each other and make a single unit.

2) Runtime errors

A runtime error occurs when the application attempts to perform an operation, which is not allowed at the runtime. An example of such an error is division by zero. A runtime error is also known as exception. All the exceptions are defined in the predefined set of classes.

3) Logical errors

A logical error occurs when the application compiles and runs correctly, but does not provide user with the expected output or results.

Exception Classes

There are many exception classes, which are directly or indirectly derived from the System.Execption class. Some of the classes are derived form the System.Exception class are the System.ApplicationException and System.SystemException classes.

The hierarchy of exception classes is displayed in the following diagram.

If any user defined application requires its own exception, it should inherit the exception from the ApplicationException class.

The System.SystemExecption class is the base class for all the predefined system exceptions. The following table describes some of the classes derived from the System.SystemException class.

Exception Classes

Description

System.IO.IOException

It handles the I/O errors.

System.IndexOutOfRangeException

It handles the errors generated when a method refers to an array element, which is out of bound.

System.NullReferenceException

It handles errors generated during the process of dereferencing a null object.

System.DivideByZeroException

It handles errors generated during the process of dividing the dividend with zero.

System.InvalidCastException

It handles errors generated during typecasting.

System.OutOfMemoryException

It handles memory allocation to the application errors.

20.2 Implement the try/catch block

User needs to handle an exception in the program by using exception handler. The handler processes the exception. In exception handling, the application is divided into the block of code. A block that shows the probability of raising an errors contains one or more exception handlers. These handlers follow a structure for handling the system and application level errors.

The following code snippet shows how an exception is raises in an application.

class Program
{
    public static void Divide ( int no1, int no2 )
    {
        int rem = no1/no2;
        Console.WriteLine(rem);
    }
    static void Main ( string[ ] args )
    {
        Program.Divide ( 20, 0 );
        Console.Read();
    }
}

In the above code snippet the DivideByZeroException is thrown. The exception is thrown when an integer value is divided by zero. When the divide statement is executed, it returns a runtime error. The program is not guarded for exceptions, the default exception handler is invoked. The default exception handler displays the error message and allows user to troubleshoot the problem.

C# provides a structured solution to the error handling problem in the form of try and catch blocks. Using these blocks, the program statements can be separated from the error handling statements.

The blocks for exception handling can be implemented by using the following keywords.

1) Try Block

2) Catch Block

3) Finally Block

1) Try Block

The try block is used to protect the statements that can throw an exception. The syntax of the try block is as shown below:

   try
   {   
      //statements that may cause an exception
   }

The try block checks the statements that are enclosed within the block and the scope of the exception handlers associated with it. A try block must have at least one catch block.

2) The Catch Block

User can associate an exception handler with the try block providing one or more catch blocks, immediately after the try block. The syntax for the catch block is as shown below:

   try
   {
        //statements that may cause an exception
   }
   catch ( … )
   {
        //error handling code
   }

The catch statement of the catch block takes an object of the exception class as a parameter, which refers to the raised exception. When the exception is caught, the statements within the catch block are executed.

The following code snippet shows the use of try catch block in an application.

class Program
{
    public static void Divide( int no1, int no2 )
    {
        try
        {
            int rem = no1 / no2;
            Console.WriteLine( rem );
        }
        catch ( DivideByZeroException e )
        {
            Console.WriteLine ( “Exception caught { 0 }”, e );
        }
            Console.WriteLine ( “Exception is handled” );
    }
        static void Main ( string[ ] args )
        {
            Program.Divide ( 20, 0 );
            Console.Read();
        }
}

The output for the code is as shown below:

20.3 Using Finally block

The finally block is used to execute a given set of statements, whether an exception is thrown or not. For example, a file needs to be closed whether an exception is raised or not. User can place the code to close the file in the finally block. The syntax to declare the finally block is as shown below:

    try
    {
        //statements that may cause an exception 
    }
    catch ( … )
    {
       //error handling code
    }
    finally
    {
       //statements to be executed
    }

 

The catch block is used to handle the exceptions of the try block. The finally block is used to guarantee the execution of statements, whether an exception occurs or not. User can have only one finally block for each try block. It is not mandatory to have a finally block after a try block.

The following code shows the use of finally block.

class Program
{
    public static void Divide ( int no1, int no2 )
    {
        int rem = 0;
        try
        {
            rem = no1/no2;
            Console.WriteLine(rem);
        }
        catch ( DivideZeroException e )
        {
            Console.WriteLine ( “Exception caught { 0 }”, e );
        }
        finally
        {
            Console.WriteLine ( “Result is { 0 }”, rem );
        }
    }
    static void Main ( string[ ] args )
    {
        Program.Divide( 20, 0 );
        Console.Read();
    }
}

The output for the code is as shown below:

The code in the finally block is executed, the result is displayed.

20.4 Implementing the User – Defined Exceptions

User can create own exception classes. Sometimes user wants to catch an exception, handle exception, and passing the exception to the calling code. These exceptions are known as user defined exceptions. The Exception class should be the base class of all the exceptions in C#. The user defined exception classes must follow the hierarchy of the exception or one of the standard classes.

Creating User – Defined Exceptions

User defined exceptions classes are derived from the ApplicationException class.

Consider the following code snippet of user defined exception.

public class Calculate
{
    int sum = 0;
    int count = 0;
    float average;
    public void DoAverage()
    {
        if ( count = = 0)
        throw ( new CountIsZeroException (“ Zero count in DoAverage” );
        else
        average = sum / count;
    }
}

public class CountIsZeroException : ApplicationException
{
    public CountIsZeroException ( string message ) : base ( message )
    {
    }
}
class Program
{
    static void Main ( string[ ] args )
    {
        Calculate  c = new Calculate();
        try
        {
            c.DoAverage();
        }
        catch ( CountIsZeroException e )
        {
            Console.WriteLine( “CountIsZeroException : {0}”, e );
        }
        Console.Read();
    }
}

To implement user defined exceptions you need to raise your exception and throw an object of the user defined exception class.

Raising Your Own Exceptions

User can use the throw statement to raise own exceptions, as shown in the following code:

public void DoAverage()
{
    if ( count == 0 )
    throw ( new CountIsZeroException ( “Zero count in DoAverage” ) );
    else
        average = sum/count;
}

In the above code, the throw statement is used to raise the user defined exception CountIsZeroException. If the value of the count variable becomes zero while calculating the average, then the application raises the CountIsZeroException to notify the user that the value of the count variable is zero in the DoAverage() method.

A message string is passed as a parameter with the object of the user defined exception class in the throw statement. This message string is displayed to the user when the user defined exception is raised.

Throwing Objects

User can throw an object of an exception class by using the throw statement. The object should be directly or indirectly derived from the System.Exception class.

The following code snippet shows the use of the throw statement in the catch block to throw an object of the current exception.

    catch ( Exception caught )
    {
       throw caught 
    }

 

Like us on Facebook