Sunday, November 30, 2008

Some ramblings on basic Exception Handling

I'd like to show two examples of really bad Exception Handling which I came across the last months.

Example 1

Try{
Foo();
}catch(Exception ex)
{
}

Please don't ever do this. It makes your software a pain in the ass to debug. The user, your co-developers and yourself will have to waste a lot of time finding the reason of some unexpected behaviour (= a bug).

Example 2

Try{
Foo();
}catch(Exception ex)
{
LabelErrorMessage.Text = "Some error occured".
LabelErrorMessage.Visible = true;
}

I agree, you can't just give the user an ex.ToString(). But this errormessage really sucks.

You should try to give the user a meaningful errormessage.
For example: This action went wrong. Here a few reasons why this might have happened. Please contact us and tell us what you were doing when it went wrong. We will try to fix it.

Or even better, use logging in the background. I have some experience with log4net and I think it's a really good system. You can show the user a non-nerdy error message and log all the nerdy stuff in the background to a database or textfile.

No comments:

Post a Comment