Exceptions

Exceptions

 

Error handling in C# is accomplished using exceptions instead of other more awkward kinds of error checking. The thrust of exception handling is that you enclose the statements that could cause errors in a try block and then catch any errors using a catch statement.

 

try {

//Statements

}

catch (Exception e) {

//do these if an error occurs

}

finally {

//do these anyway

}

 

Typically, you use this approach to test for errors around file handling statements, although you can also catch array index out of range statements and a large number of other error conditions. The way this works is that the statements in the try block are executed and if there is no error, control passes to the finally statements if any, and then on out of the block. If errors occur, control passes to the catch statement, where you can handle the errors, and then control passes on to the finally statements and then on out of the block.

 

The following example shows testing for any exception. Since we are moving one element beyond the end of the ArrayList, an error will occur:

 

try {

//note- one too many

for(int i = 0; i <= arl.Count ; i++)

Console.WriteLine (arl[i]);

}

catch(Exception e) {

Console.WriteLine (e.Message );

}

 

This code prints out the error message and the calling locations in the program and then goes on.

0123456789Index was out of range.

 

Must be non-negative and less than the size of the collection.

Parameter name: index at

 

System.Collections.ArrayList.get_Item(Int32 index)

 

By contrast, if we do not catch the exception, we will get an error message from the runtime system and the program will exit instead of going on. Some of the more common exceptions are shown in Table 1.

 

AccessException

Error in accessing a method or field of a class.

ArgumentException

Argument to a method is not valid.

ArgumentNullException

Argument is null

ArithmeticException

Overflow or underflow

DivideByZeroException

Division by zero

IndexOutOfRangeException

Array index out of range

FileNotFoundException

File not found

EndOfStreamException

Access beyond end of input stream (such as files)

DirectoryNotFoundException

Directory not found

NullReferenceException

The object variable has not been initialized to a real value.

Table 1.

 

Multiple Exceptions

You can also catch a series of exceptions and handle them differently in a series of catch blocks.

 

try {

for(int i =0; i<= arl.Count ; i++) {

int k = (int)(float)arl[i];

Console.Write(i + " "+ k / i);

}

}

catch(DivideByZeroException e) {

printZErr(e);

}

catch(IndexOutOfRangeException e) {

printOErr(e);

}

catch(Exception e) {

printErr(e);

}

 

This gives you the opportunity to recover from various errors in different ways.

 

Throwing Exceptions

You don't have to deal with exceptions exactly where they occur: you can pass them back to the calling program using the Throw statement. This causes the exception to be thrown in the calling program:

try {

//statements

}

catch(Exception e) {

throw(e); //pass on to calling program

}

 

Note that C# does not support the Java syntax throws , that allows you to declare that a method will throw an exception and that you therefore must provide an exception handler for it.

 

 

Shashi Ray
Ebook Download
View all
Learn
View all