Export (0) Print
Expand All

Handling Exceptions in Your Component

Visual Studio .NET 2003

If your component uses additional components, it will likely receive exceptions from them from time to time. Good coding practice dictates that you anticipate potential problems and write error-handling code to deal with predicted situations. The .NET Framework provides structured error handling with the Try...Catch...Finally statements. Using this method of error handling, it is possible to catch multiple exceptions and provide different error handling for each using multiple Catch statements. The Finally statement allows you to specify code that will be executed before the error-handling block is exited.

Occasionally, it will be necessary to return an exception from a secondary component to the client application, either by wrapping it in a new exception, or by throwing a custom exception. Recommendations for these practices are discussed in Recommendations on Handling Exceptions in Components.

When writing exception-handling code for your component, try to anticipate what exceptions might occur and write appropriate code to deal with them. In the event that an exception cannot be handled, you should determine what code must be executed before passing on the exception (such as closing a file, saving changes, and so on) and write the appropriate code to the Finally block.

To handle exceptions in your component

  1. Enclose calls to secondary components within the context of a Try block.
  2. Determine what exceptions are likely to be thrown by this call, and devise protocols for dealing with them.
  3. Enclose these protocols within separate Catch blocks.
  4. Create an additional Catch block to handle any unanticipated exceptions, and pass them on to the client application.
  5. Determine what code must be run, even in the event of an exception, and enclose that in a Finally block. This code will execute whether an exception is thrown or not.

The following example shows how to create a simple exception handler:

' Visual Basic
Public Sub ExceptionDemo
   Try
      ' Calls a method in a secondary component.
      SecondaryComponent.MyMethod()
   Catch e as ArgumentOutOfRangeException
      ' Insert code to fix this specific exception instance.
   Catch e as UnauthorizedAccessException
      ' Insert code to fix this specific exception instance.
   ' This block will catch any other exceptions that are thrown and
   ' package them inside a new exception which will be thrown to the
   ' client application.
   Catch e as Exception
      Throw New Exception("An unhandled exception occured", e)
   ' The Finally block specifies code that will be executed in the event
   ' of an exception. 
   Finally
      ' Insert code to save data, release references, and so on.
   End Try
End Sub

// C#
public void ExceptionDemo(int a)
{
   // C#
   try
   {
      secondaryComponent.MyMethod();
   }
   catch (ArgumentOutOfRangeException e)
   {
      // Code to fix this specific exception.
   }
   catch (UnauthorizedAccessException e)
   {
      // Code to fix this specific exception.
   }
   // This block will catch any other exceptions that are thrown
   // and package them inside a new exception which will be thrown
   // to the client application.
   catch (Exception e)
   {
      throw new Exception("An unhandled exception occurred.", e);
   }
   // The finally block specifies code that will be executed in the 
   // event of an exception
   finally
   {
      // Insert code to save data, release references, and so on.
   }
}

See Also

Structured Exception Handling | Recommendations on Handling Exceptions in Components | Exception Handling Statements

Show:
© 2014 Microsoft