Exception Handling

Exception:

An exception is termed as an abnormal condition encountered by an application during execution.

An exception is an erroneous situation that occurs during program execution. Exceptional situation arise when an operation cannot be completed normally. When an exeception occurs in an application, the system throws an error. The error is handled through the process of exception handling.

For example, the System. IO.IOException error is thrown when you try to access an illegal stream object. Similarly, if the denominator is zero, an integer division operation throws the System. DivideByZeroException error.

Whenever an error occurs, runtime creates an exception object and sends it to the program in which the exception occurred. This action is known as throwing an exception. The exception object contains information about t the type of error.

Type of error;

There are three type of error that can occur in the application. These are:

Ø Syntax errors

Ø Run-Time errors

Ø Logical errors

Syntax error:

A syntax error occurs when compiler cannot compile code. Such an error can occur when statements are not constructed properly, keywords are misspelled, or punctuation is omitted. At compilation time all the statements in the application get connected with each other to make a single executable unit.

Run-Time error:

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

Logical errors:

A logical error occurs when an application compiles and runs properly but does not produce the expected results.

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

Exception Classes:

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

The System. ApplicationException class is forwarded by a user program, and not by the Common Language Runtime (CLR). If any user-defined application requires its own exception, it should inherit the exception from the ApplicationException class.

The System.SystemException acts as a 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

Handles I/O errors

System.IndexOutOfrangeException

Handles errors generated whenmethod refers to an array element, which is out of its bound.

System.NullReferenceException

Handles errors generated duringthe process of dereferencing a null object.

System.DivideByZeroException

Handles errors generated duringthe process of dividing the dividend with zero

System.InvalidCastException

Handles errors generated duringtypecasting

System.OutOfMemoryException

Handles memory allocation tothe application errors.

Exception Handling:

Exception handling is process of providing an alternative path of execution when the application is unable to execute as desired.

You need to handle an exception in your program by using an exception-handler. This handler processes the exception. In exception handling, the application is divided into blocks of code. A block that shows the probability of raising an error contains one or more exception handler. These handlers follow a control structure and a uniform way of handling the system level and application level errors.

C# provides the structured solution to error-handling problems in the form of try and catch blocks. Using these blocks the core program statements can be separated from the error-handling statements. The core program statements handle normal program flow. You can also use the finally block to handle errors. These blocks for exception-handling can be implemented using the following keywords:

Ø try

Ø catch

Ø finally

The try Block:

The try block guards statements that may throw an exception.

Try

{

//statements that may cause an exception

}

The try block governs statements that are enclosed within it and defines the scope of the exception-handlers associated with it. In other words, if an exception occurs within the try block, an appropriate exception-handler that is associated with the try block handles the exception. A try block must have at least one catch block.

The catch Block:

You can associate an exception-handler within the try block by providing one or more catch handlers, immediately after the try block.

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 parameter, which refers to the raised exception. When the exception is caught, the statements within the catch block are executed.

The finally Block:

The finally block is used to execute a given se of statements, whether an exception is thrown or not thrown. For example, a file has to be closed whether an exception is raised or not. You can place code to close the file in both the try and catch block,

Try

{

//statements that may cause an exception

}

Catch (…)

{

//error handling code

}

Finally

{

//statements to be executed

}

The catch block is used to handle exceptions that occur in a try block. The finally block is used to guarantee the execution of statements, regardless of whether an exception has occurred or not occurred. You can have only one finally block for each try block. It however not mandatory to have finally block after a try block.

User-defined exception handling:

To distinguish between error conditions programmatically, you can create user-defined exceptions. For example, while creating File Transfer Protocol (FTP) component to perform FTP tasks, you may want to represent the FTP-related errors as specific exception types. The application that uses the FTP component can differentiate the FTP-related exception from the other exceptions and perform possible remedial tasks accordingly. In such cases, you can define custom exception classes to represent the possible type of exceptions that may arise.

For example,