Throwing and Catching Exceptions

In .NET languages such as C# and VB.NET, all exceptions will be of either type System.Exception or types derived from System.Exception. The CLR System namespace includes a number of exception types that can use your program. These exception types include ArgumentNullException and InvalidCastException. You can guess their use based on their name. For example, an ArgumentNullException is thrown when an argument to a method is null when null is not an expected (or acceptable) value.

The Throw Statement

To signal an abnormal condition in a .NET program, throw an exception. To do this, use the keyword throw. The following line of code creates a new instance of System.Exception and throws it:

image with no caption

throw new System.Exception(  );

image with no caption

Throw New System.Exception(  )

If you throw an exception and no try/catch block can handle it, your program will come to a grinding halt and display an ugly message, as shown in Figure 21-2.

Throwing an uncaught exception

Figure 21-2. Throwing an uncaught exception

Note

To reproduce this ugly situation for yourself, create the form as shown, and in the click event handler for the Throw button, add the code:

image with no caption

throw new System.Exception(  );

or download the program NoTryCatch from the web site.

The Try and Catch Statements

To handle exceptions, take the following steps:

  1. Execute any code that you suspect might throw an exception (such as code that opens a file or allocates memory) in a try block.

  2. Catch any exceptions thrown in a catch block.

In C#, a try block is created with the keyword try, and the block is enclosed in braces.

In VB.NET, a try block is created with the keyword Try and ended with the keywords End Try.

In C#, a catch block is created using the keyword catch, and is also enclosed within braces.

In VB.NET, a catch block begins with the keyword Catch and can be terminated by either the next use of the Catch keyword or the End Try statement. Catch statements can be written to catch either specific types of exceptions or all thrown exceptions.

The finally statement

In some instances, throwing an exception and unwinding the stack can create a problem. For example, if you have opened a file or otherwise committed a resource, you might need an opportunity to close the file or flush the buffer.

If you want to take action, such as closing a file, regardless of whether an exception is thrown, you have two strategies to choose from. One approach encloses the dangerous action in a try block and then closes the file in both the catch and try blocks. However, this duplication of code is ugly and error prone. C# and VB.NET provide a better alternative in the finally block.

The code in a finally block is guaranteed to be executed regardless of whether an exception is thrown.

Tip

The exact syntax of try, catch and finally blocks are language dependent, but the essential functionality is similar across languages. For further details on creating and using try, catch, and finally blocks, see Programming C# or Programming Visual Basic .NET, both by Jesse Liberty (O'Reilly).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset