tags:

views:

237

answers:

7

Hi, I don't know what we can do with a custom exception, what we can't do with a built-in one. It seems a naive question but I really have no idea about that. What do you think?

+3  A: 

There's only a few exceptions in .NET that are treated in a special manner, like ThreadAbortException, which can't (normally) be caught and handled and swallowed.

Other than that, exception types are just exception types. You can do pretty much the same with your own exceptions that you can do with the ones defined in the framework.

Lasse V. Karlsen
+8  A: 

It is useful to create a custom exception if:

  • There is no built-in exception that expresses the type of error condition you have.
  • You want to catch only that specific type of exception and not exceptions coming from the framework.

But usually if there already is an exception in the framework that you could use then it is better to use it instead of creating your own exception for the same thing.

Mark Byers
+6  A: 

You could use it for implementing special error handling for things related to your application. Suppose you build a banana application, then you could have an OutOfBananasException. If your application gets out of bananas you can throw the exception and catch it later on with special error handling.

try
{
    EatBananas();
}
catch(OutOfBananasException oobe)
{
    GetMoreBananas();
}
catch(Exception e)
{
    TellUserAndAbort();
}

Edit:
The reason to use your own Exceptions instead of the built in is to make it clear to everyone reading you code or using your library what type of error has occurred. You should only create your own exceptions when you can not find any suitable built in exception.

Edit2:
One thing you can do with your own exceptions that you can not do with built in is to add properties describing things about the error condition that the error handler might use. If you have an exception relating to customers you exception could have properties for customer name and customer id and thus make it possible for the error handler to display informative error messages to the user.

Albin Sunnanbo
I am throwing this exception this morning when I sit down to eat my cereal! hehe
Andrew Barber
I like you it the way you use 'banana' for example ^_^
Nam Gi VU
Great. Now I'm hungry. 99% of the time I make my own exception class I end up removing it as useless. In the example above I might end up making eatBananas() return a boolean success and not throw the exception. Of course, what if there were 3 possible responses...
Tony Ennis
Me too, Tony; I can't recall a custom exception class I've actually ended up using. But I don't know; ArgumentNullException just doesn't seem an adequate replacement for OutOfBananasException!
Andrew Barber
+11  A: 

The reason for the different types of exceptions is to allow you to be able to catch just the ones you want with your handlers, letting the others go on up the stack. So you can arrange to catch exceptions for certain, occasionally-expected situations just by the type of the exception.

You may not need to create your own very often at all, actually. But if you do, it would be because you need to be able to throw and capture an exception type more specific than what is available, and perhaps with additional information attached.

Andrew Barber
+2  A: 

Custom exceptions allow you to do 2 things:

  1. Capture custom typed-data in the exception
  2. Capture a custom exception occurrence

You should only create a custom exception when there isn't a built in exception to handle as necessary.

As an example, in our application we have DataLayerException which is thrown when the datalayer encounters an error (and includes the specific DBMS exception as an inner exception).

We also have DataLayerSingleResultNoneException - which is when we expect a single result back but there is no result, and DataLayerSingleResultManyException which is when we expect a single result but get many back. This allows us to catch the different problems an action them accordingly.

ck
+3  A: 

The benefits of custom exceptions have been outlined here, but before you create your own make sure the BCL doesn't already have one fitting your needs:

http://mikevallotton.wordpress.com/2009/07/08/net-exceptions-all-of-them/ (There's 141 of them!)

ohadsc
+3  A: 
supercat