What is the best method to distinguish between two exception of the same type but thrown for different reasons.
For example InvalidOperationException
can be thrown attempting to access an empty sequence, but it can also be thrown when using the Concurrent object (e.g. BlockingCollection
)
From my reading I've taken the conclusion that using the BlockingCollection.CompleteAdding()
to signal completion is perfectly fine and as such one would need to catch the Exception and handle appropriately (exit the task for instance)
What is the best way to filter these without having to use the message content?
Edit: They both appear to have the same HResult value.
AFAIK you can differentiate between two Exception
s of the same Type
only by the Message
.
However, you should not rely on the Message
to make Catch
decisions. The Message
may be localized depending on the locale configuration of the system you are executing your code on. This will make your Catch
When
block not relevant if the language of the system is different than the language of the Message
you hard-coded.
The only case where you can use the Message
is when you are the system administrator and writing a software for a server you will be managing and you are sure that the language you are using will not change in the future (which cannot be guaranteed when you write a software for to run on client devices).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With