I have repeatedly found myself in situations where I want to catch an exception (in .net), but there may be multiple exceptions with different messages that I want to handle differently.
When I view the exception in a debugger, I usually cannot find any piece of metadata that shows me a unique number representing that particular exception, so I end up writing string comparisons. The exception class is the same.
This is tedious and just seems wrong. Am I missing some good way to uniquely identify exceptions without doing a string comparison? Or do I just happen to work with some bad libraries that don't throw exceptions with good metadata? (Examples so far included ADODB, and OpenNETCF).
pseudocode explaining what i'm describing:
Try
...
Catch myexception as System.Runtime.InteropServices.COMException
switch(myexception.message){
case "oneexceptionmessage":
handleOneException()
case "twoexceptionmessage":
handleTwoException()
}
End Try
End Try
- Exception types are different
- Inner exceptions are null
- HResults may be different, but aren't normally accessible as protected properties