Home > Microsoft .Net Development Tips > .NET Developer > Better exception handling in .NET
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

.NET DEVELOPER

Better exception handling in .NET


Brian Eastwood, Site Editor
10.03.2006
Rating: --- (out of 5)


.NET Essentials Channel
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


Exception handling as defined by Wikipedia is the process by which a code base reacts to changes, or exceptions, in its normal flow. Code is exception-safe if run-time failures do not lead to invalid outputs, memory leaks or unreadable data.

Like chess, exception handling is an easy concept to understand but a difficult one to master. To that end two bloggers have recently taken up the topic, offering hints for better exception handling.

The more recent post is Scott Hanselman's Good Exception Management Rules of Thumb. Hanselman offers more than a dozen tidbits of advice, including:

  • "You shouldn't throw exceptions for things that happen all the time. Then they'd be 'ordinaries.'"
  • "Don't catch exceptions you can't do anything about. It's likely if you could do something about it, it wouldn't be exceptional…"
  • "There are reasons to swallow exceptions…but they are few and far between and they should be logged if appropriate and documented liberally."

Additional hints emerge in the comments for this blog entry.

David Barkol advises, "Catch exceptions at boundaries, for example [in] WCF (wrap exceptions in FaultException), [and in] WSE (wrap exceptions in SoapException)."

Meanwhile, Jason Kemp says, "For libraries mostly: Create a static, internal ExceptionHelper class and put all the logic for the default exceptions…in that class. It makes methods more readable, especially if there are three arguments to a method, all with restrictions. And you save lots of typing."

One comment points readers to a second blog entry on the topic -- Karl Seguin's Understanding and Using Exceptions.

Seguin and Hanselman touch upon many of the same points, namely the danger of exception swallowing and sprinkling too many Try/Catch statements throughout code.

Ultimately, Seguin decides the best way to handle exceptions is to use Try/Finally statements and to incorporate global exceptions into code. The latter, he says, "lets us write, in a centralized (i.e., easy to change) way, any exception logging we want and how to display the error in a friendly manner."

More on exception handling from SearchVB.com

  • Webcast: Exception Handling and the CLR
  • Tip: Handling exceptions in Visual Studio.NET

    Rate this Tip
    To rate tips, you must be a member of SearchWinDevelopment.com.
    Register now to start rating these tips. Log in if you are already a member.




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



    RELATED CONTENT
    .NET Developer
    How to speed up Visual Studio 2008's slow WPF designer
    How to list fonts in WPF using markup extensions and data templates
    How to convince management to buy Microsoft Visual Studio 2008
    Microsoft webcast series previews new Visual Studio 2010 features
    New features in Windows 7 bring new UI considerations for developers
    User Account Control (UAC): How to develop code for standard users
    Introduction to Vista's user account control (UAC) for developers
    Visual Studio 2008 code metrics tools will help keep code clean
    Downloadable MSDN library released for VS 2008 SP 1
    Widgetbox provides good example of how to use Facebook Developer Toolkit

    .NET Framework 2.0 and Visual Studio 2005 development
    Mono 2.0 moves .NET apps to Linux - includes migration analyzer
    Using the Visual Studio 2005 DataSet Designer to build a data access layer
    SCM Visual Studio plug-in offers updated interface
    Assembly versioning in the .NET Framework 2.0
    SearchVB.com's Podcast Page
    Visual Studio 2008 Learning Guide: Multi-targeting
    On managing a .NET Framework 1.x migration
    Case Study: IdeaBlade DevForce object mapping
    Book excerpt: Printing in Visual Basic 2005
    CodeGear RAD Studio now supports .NET 2.0

    RELATED GLOSSARY TERMS
    Terms from Whatis.com − the technology online dictionary
    IronRuby  (SearchWinDevelopment.com)

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts