Catching Specific Exceptions


Exceptions are objects. They come from classes that derive from a runtime class called System.Exception. To catch a specific exception in your code you must know the class name of the exception or know the name of one of its parents ( System.Exception is a parent class to all exceptions). For example, a developer may define a general exception like AccountExceptions . Then the developer may define more specific exceptions like InsufficientFundsException and WrongAccountTypeException that derive from AccountExceptions . As someone writing exception-handling code, you can decide to catch a specific exception like InsufficientFundsException , or you can choose to catch all exceptions of the parent type: AccountExceptions . Since all exceptions derive from System.Exception, you can also decide to catch all exceptions, even ones that have nothing to do with Accounts, by writing a catch block that catches System.Exception.

To catch a specific exception:

  1. Surround the code that may generate an exception with a try block (see "Catching Exceptions" steps 1-3 earlier in this chapter).

  2. After the closing curly bracket of the try block add a new line and type the word catch followed by an open parenthesis ( .

  3. Type the name of the exception class you wish to capture followed by a space.

  4. Type a variable name. The variable will hold the exception object when the exception occurs. (Normally people use the variable name e or ex.)

  5. Add a close parenthesis ) .

  6. Add an open curly bracket { .

  7. Type the code to handle the exception.

  8. Add a close curly bracket } ( Figure 11.13 ).

    Figure 11.13 Exception handling is preferred over VBScript's On Error Resume Next. Rather than catching every exception (as you had to do with On Error Resume Next) you can decide which exceptions to catch, and let the others flow to the caller.
     //using System.Data.OleDb; void OpenDatabase() {    string cstr =    "Provider=Microsoft.Jet.OLEDB.4.0;"    + @"Data Source=c:\csvqs.mdb;";    try    {       IDbConnection conn = new       OleDbConnection(cstr);       conn.Open();    }    catch(  OleDbException e  )    {       string Msg = "Database Error:<br>";       Msg += e.Message;       Response.Write(Msg);    } } 

graphics/tick.gif Tips

  • In its documentation, Microsoft is very careful to point out what exceptions a certain piece of code may generate for their system classes.

  • Exception classes normally have the word Exception as the last part of their name.

  • Instead of catching only one kind of exception, you can catch a number of exceptions. That's easily done by adding multiple catch blocks ( Figure 11.14 ).

    Figure 11.14 The C# compiler makes sure that you order the block from more specific exceptions to more general exceptions. That's because as soon as the runtime finds a matching exception block it will use that one and not consider the rest.
     //using System.Data.OleDb; void OpenDatabase() {    string cstr =    "Provider=Microsoft.Jet.OLEDB.4.0;"    + @"Data Source=c:\csvqs.mdb;";    try    {       IDbConnection conn = new       OleDbConnection(cstr);       conn.Open();    }    catch(  OleDbException e  )    {       string Msg = "Database Error:<br>";       Msg += e.Message;       Response.Write(Msg);    }    catch(  System.Exception e  )    {       string Msg = "General error:<br>";       Msg += e.Message;       Response.Write(Msg);    } } 
  • When you add multiple catch blocks, make sure to order them from more specific to more general. The compiler will issue an error if you don't. This must be done because the runtime simply chooses the first handler that applies to the exception. Thus if you were to write two handlers, one for a specific exception like InsufficientFundsException , and one for a more general exception like AccountException , and you put the general handler first, the runtime would always choose that handler and not the more specific handler.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net