Skip to content

Don’t Reinvent the Wheel: Exceptions You Should Not Recreate

Here’s a list of common .NET exceptions you can throw when necessary. You should not create your own exception classes when an existing class will do.

{ 4 } Comments

  1. Brad | March 28, 2005 at 3:48 pm | Permalink

    Why not subclass these. Then I can tell mine from the system generated ones.

    Why would I raise a system exception?

  2. Craig Stuntz | March 28, 2005 at 4:02 pm | Permalink

    You should only create an exception class (including subclasses of these) when you have a need for it. In other words, you (or a developer using your assembly) will be catching the exception somewhere, or you need to attach new properties to the exception.

    You can tell when an exception is "yours" by looking at the call stack. You don’t need to subclass to get this info.

    The reason to raise a system exception is that other developers will know how to handle them. If you write a method and need to throw when a parameter is null/nil, you can throw either your own exception or System.ArgumentNullException. If you throw the latter (System.ArgumentNullException) then anyone who has written a global exception handler will already deal with this correctly. If you create your own exception class, you have added no information and you may be requiring folks using your code to do extra work.

    Creating a duplicate exception class, in other words, is more or less analogous to copying code with cut and paste instead of encapsulating common operations in a single method.

  3. Brad White | May 18, 2005 at 10:09 am | Permalink

    > You can tell when an exception is "yours" by looking at the call stack. You don’t need to subclass to get this info.

    "Need" may be stretching it, but it certainly is more convenient than writing code to look at and interpret the call stack.

    >The reason to raise a system exception is that other developers will know how to handle them. If you write a method and need to throw when a parameter is null/nil, you can throw either your own exception or System.ArgumentNullException. If you throw the latter (System.ArgumentNullException) then anyone who has written a global exception handler will already deal with this correctly. If you create your own exception class, you have added no information and you may be requiring folks using your code to do extra work.

    I don’t know what you are talking about Craig. If I subclass ANE, then any exception handler should handle it transparently. Where is the extra work?

  4. Craig Stuntz | May 18, 2005 at 10:26 am | Permalink

    I don’t think subclassing ArgumentNullException is a terrible thing, if you feel you have a need for it. I would have a problem with creating a sibling class. I wouldn’t do it myself for the reasons I stated, though.

Post a Comment

Your email is never published nor shared. Required fields are marked *

Bad Behavior has blocked 1846 access attempts in the last 7 days.

Close