Exceptions in C# - Visual Explained
(Page 2 of 4 )
The array, a , contains 3 elements at positions 0, 1, and 2. Since there is no fourth element, the program will trigger an error when it tries to execute the line a[3] = 40;
In exception terminology, we say that an exception was thrown at the line a[3] = 40;
Because our program did not contain any error handling code, the exception (in this case IndexOutOfRangeException) is bubbled up all the way to the runtime, which handles the exception, prints it on the screen, and terminates the program.
We can improve this program by handling the exception ourselves. To do this, we surround the code that could cause an exception with a try block, and catch the exception after the try block as such:
class Program
{
static void Main( string [] args)
{
int [] a = new int [3];
try
{
a[0] = 10; //ok
a[1] = 20; //ok
a[2] = 30; //ok
a[3] = 40; //No such element.
}
catch ( Exception e)
{
Console .WriteLine( "Error: accessing an element " +
"outside the bounds of the array!" );
}
}
}
So, after we surrounded the code with the try block, we added a catch that catches objects of type Exception. Since the Exception class is the parent of all exceptions, the catch block will catch any exception that might happen in the try block (not necessarily array-related exceptions). The code execution stops at the offending statement (a[3] = 40;), and falls down to the catch blocks (in this case, there is only one).
If the catch object matched the one being thrown, then the catch block is executed and all other catch blocks are skipped (nothing to skip in this case, because there is only one catch block). The execution is then transferred to any code after the catch blocks. Please note that the exception being thrown is ArrayIndexOutOfBoundException and yet it is caught by an Exception type. This happens because ArrayIndexOutOfBoundException is of type Exception (is-a relationship).
As mentioned earlier, an Exception object (or any of its descendants) holds very helpful details about the cause of the error. Let's modify the previous code to print out more details about the exception:
class Program
{
static void Main( string [] args)
{
int [] a = new int [3];
try
{
a[0] = 10; //ok
a[1] = 20; //ok
a[2] = 30; //ok
a[3] = 40; //No such element at position 3.
}
catch ( Exception e)
{
//printing the exception details
WriteExceptionDetails(e);
}
}
static void WriteExceptionDetails( Exception e)
{
Console .WriteLine( "Message: {0}" , e.Message);
Console .WriteLine( "Source: {0}" , e.Source);
Console .WriteLine( "StackTrace: {0}" , e.StackTrace);
}
}
We factored out the code that prints exception information into a separate method called WriteExceptionDetails(). This method takes an Exception object as a parameter and prints out its details. This is helpful, since we can call this method anywhere in the code for any Exception object. In this case, we simply wrote the details to the output screen. Instead, we could have logged the details into a file that we can analyze later to better improve our code. The Exception object contains properties, such as Message (a nicely formatted message about the cause of the error), Source (the name of the program where the exception happened), and StackTrace (the sequence of calls that triggered the exception).
Next: InnerException >>
More C# Articles
More By Ayad Boudiab