Monday 20 August 2018

C#: Exception Handling

Exception is an event that disrupts the normal flow of execution.

When an error occurred in a method, then the method creates an exception object, and handle it to the C# Run time system. The exception object contains information about exception type, the line where the exception occurred, stack trace etc.,

C# provides 3 blocks to handle the exception that may occur in the program.

Those are:
1. try block
2. catch block
3. finally block

Whenever you felt that, there is a possibility of occurring an exception in the code, then place the entire code in the try block. Provide handlers to handle the exception.

After a method throws an exception, the run time system tries to find exception handlers to handle it.

If the Run time system finds a handler to handle the exception, then it executes the block of code in the handler, and Proceed execution from the catch block onwards. Otherwise program simply terminates.

Program.cs
using System;

class Program
{
    static void print()
    {
        int[] arr = new int[2];
        arr[10] = 20;
    }

    static void callPrint()
    {
        print();
    }
    static void Main(string[] args)
    {
        callPrint();
    }
}

Run above program, you will end up in ‘IndexOutOfRangeException’.

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Program.print() in C:\Users\hari\Documents\Visual Studio 2015\Projects\HelloWorld\HelloWorld\Program.cs:line 8
   at Program.callPrint() in C:\Users\hari\Documents\Visual Studio 2015\Projects\HelloWorld\HelloWorld\Program.cs:line 13
   at Program.Main(String[] args) in C:\Users\hari\Documents\Visual Studio 2015\Projects\HelloWorld\HelloWorld\Program.cs:line 17


As you observe the above error message, below things are clear.

1. Exception Type is : IndexOutOfRangeException: : Index was outside the bounds of the array.

2. Program.print() in C:\Users\hari\Documents\Visual Studio 2015\Projects\HelloWorld\HelloWorld\Program.cs:line 8

Above line tells that exception occured in the method print.

3. Stack trace give the detailed view to find the root cause of the exception.

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Program.print() in C:\Users\hari\Documents\Visual Studio 2015\Projects\HelloWorld\HelloWorld\Program.cs:line 8
   at Program.callPrint() in C:\Users\hari\Documents\Visual Studio 2015\Projects\HelloWorld\HelloWorld\Program.cs:line 13
   at Program.Main(String[] args) in C:\Users\hari\Documents\Visual Studio 2015\Projects\HelloWorld\HelloWorld\Program.cs:line 17

From the stack trace, we can sure that exception occurred in the method print(), which is called by callPrint(), which in turn called by the main method.

If the program don't have any exception handling mechanism, then the Java run time system throws the exception and terminates. Same thing happened in the above program.


Program.cs
using System;

class Program
{
    static void print()
    {
        int[] arr = new int[2];
        arr[10] = 20;
    }

    static void callPrint()
    {
        print();
    }
    static void Main(string[] args)
    {
        try
        {
            callPrint();
        }
        catch(IndexOutOfRangeException e)
        {
            Console.WriteLine("IndexOutOfRangeException occured");
            Console.WriteLine(e.Message);
        }

        Console.WriteLine("Finished Execution");
        
    }
}

Output
IndexOutOfRangeException occured
Index was outside the bounds of the array.
Finished Execution


As you observe the output, there is an IndexOutOfRangeException occurred, then C# run time system attempts to find out a handler to handle this exception, So it executed the statements inside the catch block. Once the statements in the catch executed, then the statements after the catch statement starts executing.






Previous                                                 Next                                                 Home

No comments:

Post a Comment