0

We use ELMAH for our ASP.NET web app and I am stumped as to some of the exceptions we get. Some of these are:

  • System.FormatException: Invalid length for a Base-64 char array.
  • System.Web.HttpException: Unable to validate data.
  • System.Security.Cryptography.CryptographicException: Padding is invalid and cannot be removed.

I simply have no idea why they occur, but the end user apparently does not see them, so I want to ignore them and supress the emails. If I do this, I want to make sure that System.FormatException only uses Invalid length for a Base-64 char array. for its message text and not also some other message. If it did and I ignored it, I might be missing out on other exceptions that are thrown under System.FormatException. If that is the case, I'd have to check for the message text. That's not a problem, but I really don't like hardcoding strings in my app.

Update:

I tried this code:

        try
        {
            throw new System.FormatException();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        Console.Read();

And its message text is:

One of the identified items was in an invalid format.

This tells that the answerer is right and that exceptions should be ignored based on both exception and message text.

oscilatingcretin
  • 9,495
  • 31
  • 111
  • 188

1 Answers1

0

The exception's Message property is not useful to help you diagnose bugs in your code or problems with the user's configuration. This is also why you don't know why they occur. You must put more information in your email, particularly the exception's StackTrace property is crucial to help you find out exactly where the exception occurred. If an exception has an InnerException then you always need to know that one as well since it is typically the core reason another exception got triggered.

Simply use the exception's ToString() method to generate a better diagnostic to put in your email message.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
  • I use ELMAH. I didn't think of the inner exception, though. I am not sure if ELMAH reports any details about inner exceptions, but I suppose it's worth looking up. Still, my question is "Does each exception type have it's own message text?". – oscilatingcretin Apr 05 '12 at 16:22
  • The answer is no, .NET doesn't guarantee it. And can't guarantee it, many messages are generated dynamically. Either by framework code or your code or some library like a database provider. Even the exact same exception caused by the exact same bug doesn't guarantee the exact same message. It may be localized. – Hans Passant Apr 05 '12 at 16:27