Sometimes the exception type is unique enough to indicate the exact issue, such as an ArgumentOutOfRangeException. Othertimes, the exception is more general and could be thrown for several reasons. In this scenario, it seems the only additional information is found in the exception message property.
In my current circumstance, I am getting a CommunicationException which throws the error message:
The maximum message size quota for incoming messages (65536) has been exceeded
As multiple different errors may be thrown via the CommunicationException, is it bad practice to use the message property to determine the cause, like so:
catch (CommunicationException ex)
{
if (Regex.IsMatch(ex.Message, "The maximum message size quota for incoming messages .* has been exceeded"))
{
// handle thrown exception
}
throw;
}
Will these messages be constant and reliable on all systems? Are there any other considerations, such as localization?
Conclusion:
My scenario with the 'CommunicationException' was a bad example, as I later realized that there was a QuotaExceededException in the InnerException property. Thanks to the responses, I knew to look for any data that existed in the exception to indicate the exact cause. In this case, it was the InnerException property.
In terms of the question regarding whether or not the message property should be used to determine the cause, it seems to be the general consensus that it should be avoided unless there is no alternative. The message property value may not remain constant over varying systems due to localization.