in

Platinum Bay

Peace, Love, and...

This Blog

Syndication


.NETicated

Exceptional Exceptions

There are many, many exceptions in the .NET framework. The base type of all exceptions is the System.Exception class.

There are a couple rules you want to follow when throwing exceptions. First, avoid throwing the System.Exception or System.SystemException base types. When you go to catch an exception, the base exception types tell you nothing about the exception type. Second, throw the most specific exception type possible. Again, this helps when you go to catch an exception to know what type of exception has been thrown, and makes the application much easier to maintain and debug. It is also preferred to throw existing framework exceptions rather than rolling your own if possible. Lastly, make sure that your exception parameters are simple and easy to understand. We all know how hard it can be to debug cryptic error messages, so make sure that yours aren't. For example, if you are throwing an exception inside of a catch block, make sure to set the InnerException parameter to the caught exception.

Sometimes it is a little daunting to figure out what exception to throw, and even know all the exception types that are available. To help me, and hopefully you in the future, following is a hierarchical list of most of the types that derive from System.Exception, linked to their respective MSDN article. I generated this list based on version 2.0 of the framework, and tried to include all the major .NET framework assemblies.

References:
http://msdn2.microsoft.com/en-us/library/seyhszts(vs.71).aspx
http://msdn2.microsoft.com/en-us/library/5b2yeyab(VS.71).aspx

Published Jun 20 2007, 10:47 AM by Steve
Filed under: , ,

Comments

No Comments

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is an independent consultant, INETA speaker, and Microsoft MVP for Visual Studio ALM. He has been working in technology for over ten years focusing on custom application development and Application Lifecycle Management. Steve is also Microsoft and IBM certified and a community fanatic having led sessions at nearly 100 events across North America. When he's not developing software solutions or engaging with the community about software technology, Steve is a closet singer and songwriter and plays the guitar and keys. Occasionally, Steve even gets to sleep. Occasionally.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.