.NET Throwing Custom Exceptions
Asked Answered
S

6

10

Can anyone shed some light on the pros and cons of throwing custom exceptions (which inherit from System.Exception), or the proper way to use them? I'm already aware of the when/when not to throw exception, but I am looking for guidance on how to create my own custom exceptions.

Summers answered 15/10, 2009 at 15:30 Comment(3)
Dupe: https://mcmap.net/q/1161577/-why-create-custom-exceptions-closedCystine
I wouldn't call this a dupe, close, but not quite in my book, maybe I just wasn't clear enough in my question.Summers
This is certainly a better worded than the linked duplicate. I will vote to reopen if it closes.Lucienlucienne
L
9

These are all great posts. So far I agree most with Brian Rasmussen -- create custom exceptions when you want to handle different types of specific exceptions.

Perhaps an example will help. This is a contrived example, and may or may not be useful in everyday code. Suppose you have a class responsible for authenticating a user. This class, in addition to authenticating a user, has a lock-out mechanism to lock out a user after several failed attempts. In such a case, you might design as part of the class two custom exceptions: AuthenticationFailedException and UserLockedOutException. Your AuthenticateUser method would then simply return without throwing if the user was successfully authenticated, throw AuthenticationFailedException if the user failed authentication, or throw UserLockedOutException if the user was locked out.

For example:

try
{
    myAuthProvider.AuthenticateUser(username, password);
    ShowAuthSuccessScreen();
}
catch(AuthenticationFailedException e)
{
    LogError(e);
    ShowAuthFailedScreen();
}
catch(UserLockedOutException e)
{
    LogError(e);
    ShowUserLockedOutScreen();
}
catch(Exception e)
{
    LogError(e);
    ShowGeneralErrorScreen();
}

Again, a contrived example. But hopefully it shows how and why you would want to create custom exceptions. In this case, the user of the AuthProvider class is handling each custom exception in a different way. If the AuthenticateUser method had simply thrown Exception, there would be no way to differentiate between the different reasons why the exception was thrown.

Lucienlucienne answered 15/10, 2009 at 15:44 Comment(0)
L
9

These are all great posts. So far I agree most with Brian Rasmussen -- create custom exceptions when you want to handle different types of specific exceptions.

Perhaps an example will help. This is a contrived example, and may or may not be useful in everyday code. Suppose you have a class responsible for authenticating a user. This class, in addition to authenticating a user, has a lock-out mechanism to lock out a user after several failed attempts. In such a case, you might design as part of the class two custom exceptions: AuthenticationFailedException and UserLockedOutException. Your AuthenticateUser method would then simply return without throwing if the user was successfully authenticated, throw AuthenticationFailedException if the user failed authentication, or throw UserLockedOutException if the user was locked out.

For example:

try
{
    myAuthProvider.AuthenticateUser(username, password);
    ShowAuthSuccessScreen();
}
catch(AuthenticationFailedException e)
{
    LogError(e);
    ShowAuthFailedScreen();
}
catch(UserLockedOutException e)
{
    LogError(e);
    ShowUserLockedOutScreen();
}
catch(Exception e)
{
    LogError(e);
    ShowGeneralErrorScreen();
}

Again, a contrived example. But hopefully it shows how and why you would want to create custom exceptions. In this case, the user of the AuthProvider class is handling each custom exception in a different way. If the AuthenticateUser method had simply thrown Exception, there would be no way to differentiate between the different reasons why the exception was thrown.

Lucienlucienne answered 15/10, 2009 at 15:44 Comment(0)
C
4

Use your own exceptions to flag errors which are specific to your application/domain. The advantage is that your catch blocks can filter the correct exceptions and act upon that. Use specific standard exceptions for everything else.

Coffey answered 15/10, 2009 at 15:33 Comment(0)
G
1

Custom exceptions allow you to provide clear, meaningful exceptions, which in turn can make your library more usable, provided you use the existing exceptions whenever appropriate.

Create a custom exception any time you need to raise an exception that doesn't fit directly in the framework's exception model.

Geneticist answered 15/10, 2009 at 15:33 Comment(0)
G
1

I recently wrote an entire blog entry on this particular subject:

The basic summary though is ...

You should only create a new exception if you expect developers to take corrective action for the problem or to log for post mortem debugging.

Gloom answered 15/10, 2009 at 15:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.