Why Re-throw Exceptions?

后端 未结 13 1727
借酒劲吻你
借酒劲吻你 2020-12-13 00:25

I\'ve seen the following code many times:

try
{
    ... // some code
}
catch (Exception ex)
{
    ... // Do something
    throw new CustomException(ex);

            


        
相关标签:
13条回答
  • 2020-12-13 01:04

    As Rafal mentioned, sometimes this is done to convert a checked exception to an unchecked exception, or to a checked exception that's more suitable for an API. There is an example here:

    http://radio-weblogs.com/0122027/stories/2003/04/01/JavasCheckedExceptionsWereAMistake.html

    0 讨论(0)
  • 2020-12-13 01:07

    I can think of the following reasons:

    • Keeping the set of thrown exception types fixed, as part of the API, so that the callers only have to worry about the fixed set of exceptions. In Java, you are practically forced to do that, because of the checked exceptions mechanism.

    • Adding some context information to the exception. For example, instead of letting the bare "record not found" pass through from the DB, you might want to catch it and add "... while processing order no XXX, looking for product YYY".

    • Doing some cleanup - closing files, rolling back transactions, freeing some handles.

    0 讨论(0)
  • 2020-12-13 01:08

    Actually there is a difference between

    throw new CustomException(ex);
    

    and

    throw;
    

    The second will preserve the stack information.

    But sometimes you want to make the Exception more "friendly" to your application domain, instead of letting the DatabaseException reach your GUI, you'll raise your custom exception which contains the original exception.

    For instance:

    try
    {
    
    }
    catch (SqlException ex)
    {
        switch  (ex.Number) {
            case 17:
            case 4060:
            case 18456:
               throw new InvalidDatabaseConnectionException("The database does not exists or cannot be reached using the supplied connection settings.", ex);
            case 547:
                throw new CouldNotDeleteException("There is a another object still using this object, therefore it cannot be deleted.", ex);
            default:
                throw new UnexpectedDatabaseErrorException("There was an unexpected error from the database.", ex);
        } 
    }
    
    0 讨论(0)
  • 2020-12-13 01:11

    The application will most probably be catching those re-thrown exceptions higher up the call stack and so re-throwing them allows that higher up handler to intercept and process them as appropriate. It is quite common for application to have a top-level exception handler that logs or reports the expections.

    Another alternative is that the coder was lazy and instead of catching just the set of exceptions they want to handle they have caught everything and then re-thrown only the ones they cannot actually handle.

    0 讨论(0)
  • 2020-12-13 01:12

    Rethrowing the same exception is useful if you want to, say, log the exception, but not handle it.

    Throwing a new exception that wraps the caught exception is good for abstraction. e.g., your library uses a third-party library that throws an exception that the clients of your library shouldn't know about. In that case, you wrap it into an exception type more native to your library, and throw that instead.

    0 讨论(0)
  • 2020-12-13 01:14

    Generally the "Do Something" either involves better explaining the exception (For instance, wrapping it in another exception), or tracing information through a certain source.

    Another possibility is if the exception type is not enough information to know if an exception needs to be caught, in which case catching it an examining it will provide more information.

    This is not to say that method is used for purely good reasons, many times it is used when a developer thinks tracing information may be needed at some future point, in which case you get try {} catch {throw;} style, which is not helpful at all.

    0 讨论(0)
提交回复
热议问题