Implication of (not) rethrowing exception after logging
In a team environment, if I handle an exce开发者_如何学Cption (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
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.
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!
精彩评论