开发者

What exception classes already exist and when do we use them?

In .net, c#

There ar开发者_C百科e many sub-classes of Exception already existing, what are they and when do we use them instead of creating our own sub-class?

This question is duplicate of c# is there an exception overview


The link provided by Jason is pretty comprehensive, but a lot of the exception types in it (such as NullReferenceException or IndexOutOfRangeException) are really only ever thrown by the framework; it would not be very appropriate for you was a developer to explicitly throw them.

Here are, in my opinion, a handful of the most useful exception types for a developer.

ArgumentNullException

This one's obvious: one of the arguments passed to a method was null, but for this particular method, a null value for that argument is not allowed.

ArgumentOutOfRangeException

A value was supplied to a method that is outside of the range that makes sense for that method.

Example

In most methods that take a parameter representing a magnitude or length, only positive values make sense for that parameter. So a check such as

if (x < 1)
{
    throw new ArgumentOutOfRangeException("x");
}

is common.

FormatException

This is a pretty reasonable choice when you're writing your own custom text-parsing method, or really any code that expects strings to match a certain format, and some input is supplied that the code can't understand.

InvalidOperationException

I tend to use this one a lot (probably overuse it, actually) whenever I'm not sure what else to use. Generally I think of this type as conveying that the client attempted to do something illegal, for reasons relevant to the current class or method.

Example

Many IEnumerator<T> implementations throw an InvalidOperationException when the collection they're enumerating is modified. This is a reasonable design choice, as it is much easier to design a collection class that does not handle this case than it is to design one that does.

NotSupportedException

This one typically makes sense in a class that derives from some base class and only offers a partial implementation of that base class's abstract members.

Example

Some developers choose to write base classes with "optional" functionality that may be provided by derived classes or may not be. Below is an example:

abstract class Animal : Organism
{
    public virtual bool EatsPlants
    {
        get { return false; }
    }

    public virtual void EatPlant(Plant food)
    {
        throw new NotSupportedException();
    }

    public virtual bool EatsAnimals
    {
        get { return false; }
    }

    public virtual void EatAnimal(Animal food)
    {
        throw new NotSupportedException();
    }
}

class Herbivore : Animal
{
    public override bool EatsPlants
    {
        get { return true; }
    }

    public override void EatPlant(Plant food)
    {
        // whatever
    }
}

Obviously this is just my own personal (and subjective) list; but I thought it might give you an idea of what kinds of exceptions you can and should be leveraging within your own code.


Here is list of common exception types. If you want to know when to create your own exceptions, then try:

What are some best practices for creating my own custom exception?


For a list of sub-classes of Exception I recommend that you use the .NET Reflector.

http://www.red-gate.com/products/reflector/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜