I am doing a small hobby project in C #, a language that I don’t know well, and stumbled upon the following:
Suppose you are using the asynchronous operation used by BackgroundWorker. Now, if there is an exception, the RunWorkerCompleted event will be raised, and the RunWorkerCompletedEventArgs.Error will be non-zero.
Is the next canonical path handling different types of exceptions? (Here, all kinds of exceptions are inherited from the WRT siblings)
if (e.Error != null) { FirstKindOfException e1 = e as OneKindOfException; SecondKindOfException e2 = e as SecondKindOfException; ... LastKindOfException en = e as LastKindOfException; if (e1 != null) { ... } else if (e2 != null) { ... } ... else { ... } }
It works, but ... it doesn't feel good.
source share