views:

61

answers:

2

Hi,

In a team environment, if I handle an exception (like so):

    protected void Page_Load(object sender, EventArgs e)
{
    this.exTest();

}


public void exTest()
{
    try
    {
        throw new Exception("sjsj");
    }

    catch (Exception ex)
    {
        string s = ex.Message;
        throw;
    }

}

What is the implication of not rethrowing the exception (throw)? Even without the keyword the custom error settings in web.config are used (redirection to specified page).

Thanks

+1  A: 

If you don't rethrow the exception, the calling code will presumably assume that the method worked. That's usually a really, really bad thing to do.

"No, I didn't manage to load your account details fully, so your bank account has the default balance of 0. Now, wait while I just write that data back into the database... In the meantime, feel free to peruse the log I helpfully generated to show just why I'm screwing up your data" ;)

Seriously, logging an error isn't equivalent to handling it. You should rethrow an exception unless you've actually handled it to such an extent that the caller can effectively assume it didn't happen.

If your error page is being shown anyway, that would suggest that even after this method failed, another one failed as well. You shouldn't rely on that though.

Jon Skeet
If you are catching the error just to log with additional information such as class and line number, then rethrow, you are adding a good bit of value even though you are not handling the exception. If you just let the exception go on up to the UI or where ever, the ex.message it brings and stack trace can make it very difficult to figure out what exactly caused the issue. Especially if your only repro is in production. You can be left with no good info.
A: 

The consequence of not rethrowing the exception is that you effectively ignore the exception. This is rarely the right thing to do as it masks errors. Ignoring errors could put your application in not well defined state with all sorts of strange side effects. In short: Don't go there!

Brian Rasmussen