Welcome to my blog - Ashish

I just started liking writing. Did I write that correctly ??

.NET Exceptions

I recently attended the “Debugging .NET Applications” course by Jhon Robbins. In contrast to my expectations, the course was not at all like his books. I was a great fan of his books but really got disappointed with the course. He is a really nice person, he will crack nice jokes, make you laugh, but in the end, the course was not delivering what you expect. His demos were not working, the topics were simply overview and he was not talking the class with him.

Some of you might totally disagree with me and I can understand. He is a great author.

Inspite of all the disappointment, I did find some topics useful and one of them was .NET Exceptions. There were some good points which are listed below.

The idea of exception handling is to give a single place for all the clean up in case of unexpected conditions. .NET exceptions are totally different from C/C++ exception where there was no base class for exception but in .NET. all the exceptions were derived from System.Exception class.

Here are some do's and dont's for .NET excptions.

  1. Never ever throw or catch System.Exception. - The idea is, you should catch what you are throwing, or you should catch if you are calling some function which is throwing an exception because of your wrong input. You should never catch any exception which is coming because of some other reason, like low memory condition etc.
  2. Never throw these exceptions - System.Exception (already covered), System.ApplicationException, System.SystemException, System.NullReferenceException, System.IndexOutOfRangeException.  All these exceptions can also be thrown from CLR so there would be now way if you throw any of the above exception and catch, weather the exception was yours or not. Always throw a more specific exception rather than a generic one.
  3. Never throw the exception you caugth by calling throw exp (where exp is the exception you caught).  This will loose the stack trace. Rather, either call throw, which will rethrow the caught exception with proper stack trace, or create a new exception by setting the innerexception to the exception you just caught. This way, the caller will know the exact stack where the original exception was caused.
  4. Always drive your exception from System.Exception, not from System.ApplicationException. (This is supposed to be from latest Design Guidelines)
  5. Drive a new exception only if necessary. If its a generic exception type, throw the existing defined exception. for ex - System.ArgumentNullException.
  6. When driving your own exception, make sure you provide at least these 3 constructors. 1. parameterless (default) constructor. 2. A single string parameter to set the message. 3. A string and an instance of an exception derived type, so that you can pass the inner exception in the constructor.

In addition to this, exception should be thrown only when there was a voilation of contract between the caller and callee. Is should not be used as a way of communicating results. Remember, exceptions means unexpected conditions where the execution cann't be continued.

I found these points useful and thought some of you might find them useful as well. Let me know if you have some more generic guidelines which can be used while writing code.

-Ashish

Published Saturday, May 15, 2004 2:51 AM by ashgupta

Comments

 

Thomas Tomiczek said:

::Never throw these exceptions -

Let's see:

::System.IndexOutOfRangeException

WRONG.

It is absolutly legal to throw them. Have you EVER implemented an IList or an indexer with an inxex going in?

Perfectly legal to throw an IndexOutOfRangeException when - the index is out of range.
May 15, 2004 9:03 AM
 

Kirk Allen Evans said:

Thomas is right here. You sure you didn't possibly take some bad notes, or miss some of the context that John used in this discussion? Richter, also of Wintellect, helped build the guidelines for exception handling for the patterns and practices group, and the PAG information is consistent with chapter 7 of his book. I find it difficult to believe that Robbins simply said "don't throw IndexOutOfRangeException" unless he qualified it with a specific example.

IndexOutOfRangeException is OK to throw. In fact, it is OK to throw any type of exception (although you should throw meaningful exceptions). You can throw System.Exception, although it is not a best practice because you don't convey anything meaningful. The real problem is catching exceptions.

You should not catch System.Exception, as noted above, because of the 3 special exceptions that the runtime may throw at any time: StackOverflowException, OutOfMemoryException, and ExecutionEngineException.

I have several posts on exceptions:

http://blogs.xmladvice.com/kaevans/archive/2004/02/04/439.aspx

http://blogs.xmladvice.com/kaevans/archive/2004/02/18/555.aspx

The patterns and practices guide that I mentioned on exception management is available at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/exceptdotnet.asp
May 15, 2004 11:18 AM
 

Ashish said:

Well, Robbins also mentioned that he get an heated discussion when ever he open the Exception part of the discussion.
I agree with you all that it might make sense to throw System.IndexOutOfRangeException in cases (as listed above), but what he meant was you should try to throw a more specific exception wherever possible. It might be the case that it's not possible to throw any more specific exception than IndexOutOfRange and in that case it may be OK to throw that. But you can also throw ArgumentOutOfRange if it make sense and that give a much better picture that one of the argument was out of range.
May 15, 2004 12:47 PM
 

Ashish said:

I also want to add that some of the things didn't make sense to me too during the course. For ex, why should you drive your exception from System.Exception rather than System.ApplicationException? He simply said that this is from the latest Internal Design Guidelines but he didn;t any reason why we should do it. Also, he pointed us to a DL where we can post our questions.

That's one of the reason I didn;t like the class very much. :(
May 15, 2004 12:54 PM
 

Kirk Allen Evans said:

Again, I think you might have taken some bad notes or misunderstood what Robbins was talking about. Derive from System.ApplicationException instead of System.Exception, since the former is designed specifically to signal application-specific exceptions, where the latter is a general base class. See the link that I provided earlier for more information, this guideline serves as the best practices guideline and has not been updated with any such information as deriving directly from System.Exception.
May 15, 2004 7:44 PM
 

Jeff Clark said:

The help for ApplicationException says "If you are designing an application that needs to create its own exceptions, derive from the ApplicationException class." The help for System.Exception would seem to agree with this where it says (as a bullet in a list) "The user-defined application exception classes derived from ApplicationException."
May 17, 2004 9:41 AM
Anonymous comments are disabled

This Blog

Syndication

Tags

No tags have been created or used yet.

Archives


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker