posted by Amit Bahree on Tue 27th Apr 2004 05:42 UTC
"Exception Management in .NET, Page 3/3"
Best Practices for “Consuming” Exceptions.
A well-designed set of error handling code blocks can make a program more robust and less prone to crashing because the application handles such errors. The following list contains suggestions on best practices for handling exceptions:
  • Know when to set up a try/catch block. For example, you can programmatically check for a condition that is likely to occur without using exception handling. In other situations, using exception handling to catch an error condition is appropriate.
  • Use try/finally blocks around code that can potentially generate an exception and centralize your catch statements in one location. In this way, the try statement generates the exception, the finally statement closes or deallocates resources, and the catch statement handles the exception from a central location.
  • Always order exceptions in catch blocks from the most specific to the least specific. This technique handles the specific exception before it is passed to a more general catch block.

  • I would highly recommend using the Exception Application Block that is provided by Microsoft. The Exception Application Block provides a simple and extensible framework for logging exception information to the Event Log. You can also create your own components, to log exception details to other data sources or to notify operators, without affecting your application code.

    This can easily be used as a building block in your own application. If you use it, you will reduce the amount of custom error handling code you need to create, test, and maintain. You will also make your application more robust and easier to debug. Specifically, the Exception Management Application Block will help you:

  • Manage exceptions in an efficient and consistent way
  • Isolate exception management code from business logic code
  • Handle and log exceptions with a minimal amount of custom code

  • For more information and references please refer to the appendix.


    Appendix
    Please note that all the information in the appendix is from MSDN and .NET SDK. It is the copyright of their respective owners.

    The following is a step-by-step description of how a first chance exception is handled:

  • The runtime informs the debugger that a first chance exception has occurred. The Debugger Interface calls Exception on the ICorDebugManagedCallback interface that the debugger registered with the runtime.
  • The debugger obtains information about the exception. The debugger calls GetCurrentException on the ICorDebugThread object it was passed in the callback to obtain an exception object (ICorDebugValue).
  • The debugger obtains the ICorDebugObjectValue object for the exception. The debugger calls QueryInterface on the ICorDebugValue object to obtain the ICorDebugObjectValue object for the exception.
  • The debugger obtains the class of the exception object that was thrown. The debugger calls GetClass on the ICorDebugObjectValue exception object.
  • The debugger decides to ignore the exception by simply continuing.
  • The runtime informs the debugger that a last chance exception has occurred. The Debugger Interface component calls Exception on the ICorDebugManagedCallback object and specifies that the exception is a “last chance” exception.
  • The user decides the exception is inconsequential. The debugger calls ClearCurrentException on the ICorDebugThread object for the current debuggee thread. This clears the exception and prevents the exception from being thrown.
  • The debugger continues execution of the process. The debugger calls Continue on the ICorDebugProcess object for the current debuggee process.

  • Exception callback sequence

    Microsoft has an excellent paper entitled Exception Management Architecture Guide.


    About the Author
    Amit Bahree is a Solution Architect in Cap Gemini Ernst and Young's Advanced Development and Integration practice. He specializes in Microsoft technologies and platforms and can be reached at amit@NOSPAM.desigeek.com. His blog can be found at http://www.desigeek.com


    If you would like to see your thoughts or experiences with technology published, please consider writing an article for OSNews.
    Table of contents
    1. "Exception Management in .NET, Page 1/3"
    2. "Exception Management in .NET, Page 2/3"
    3. "Exception Management in .NET, Page 3/3"
    e p (0)    25 Comment(s)

    Related Articles

    posted by Thom Holwerda on Fri 13th Jun 2008 18:09, submitted by wakeupneo
    posted by Thom Holwerda on Fri 22nd Feb 2008 09:16, submitted by obsethryl