229

Handling Exceptions in C# Programs

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

In this article I shall demonstrate the exception-handling capabilities of C#.


Introduction

 
After finishing the Part 1, let’s create a program that will demonstrate the exception-handling capabilities of C#.   
This program will define an array that contains only one element. 
 
The program will then prompt the user to enter a 0 or a 1. If 0 is entered, program execution performs as expected and the element at an index of zero in the array will be displayed on screen. If 1 is entered, an exception will be thrown, as there is no corresponding index in the array.
 
Step 1 of 4
 
The Main method of this program may throw an exception.
 
Enter the keyword that marks the code the program will attempt to execute.
 

 
using System; 

namespace Exceptions 
{ 
  class ExceptionHandlingTest 
  { 
    static void Main(string[] args) 
    { 
       int[] intArray = {100}; 
       MISSING CODE (You will have to enter TRY Block over here)
    } 
  } 
}

 
Result
 
You type try, which encloses the code in a method that is to be executed but may throw an exception.
 
Step 2 of 4
 
The try block contains code that will be executed and may throw an exception.
 
What will the program do if an exception is thrown?    

using System; 

namespace Exceptions 
{ 
  class ExceptionHandlingTest 
  { 
    static void Main(string[] args) 
    { 
      int[] intArray = {100}; 
      try 
      { 

       MISSING CODE (You will have to enter proper Catch Block)
      } 
    } 
  } 
}

 
Result
 
If an exception is thrown, the program will unwind the assembly until it finds a catch statement that contains an appropriate exception handler.
 
Step 3 of 4
 
To signal an abnormal condition on C#, you throw an exception.
 
Enter the line of code that creates an instance of System.Exception.
 

using System; 

namespace Exceptions 
{ 
  class ExceptionHandlingTest 
  { 
    static void Main(string[] args) 
    { 
      int[] intArray = {100}; 
      try 
      { 
                 Console.WriteLine("Enter 0 for an Exception "); 
                 Console.WriteLine("Enter 1 for IndexOutOfRangeException"); 
                 int index = Int32.Parse(Console.ReadLine()); 

                 Console.WriteLine("Number is {0}", intArray[index].ToString()); 

        MISSING CODE; (You can choose any line of code from the Results below)
      } 
    } 
  } 
}

Result
 
In fact, you can use any of the following lines of code to create an instance of System.Exception:
 

throw new Exception() 
throw new System.Exception() 
throw new Exception( ) 
throw new System.Exception( ) 
throw new Exception() 
throw new System. Exception()

 
Step 4 of 4
 
The array described in this program contains only one element.
 
Which exception will be thrown if the user chooses an element that isn’t part of the array?
 

catch (MISSING CODE)

 
Result
 
If the user tries to choose an element that isn’t part of the array, the IndexOutOfRangeException exception is thrown.
 
The following is the output of this program when the user enters 0.
 

Enter 0 for an Exception 

Enter 1 for IndexOutOfRangeException 

0 

Number is 100 

Exception of type System.Exception was thrown. 

There is only one element in this array: 100 

And this is the output if the user enters 1. 

Enter 0 for an Exception 

Enter 1 for IndexOutOfRangeException 

1 

IndexOutOfRangeException caught, 

an index of 1 does not exist in the array! 

There is only one element in this array: 100

 
Thanks for reading.

prajapat