c# - How to tell apart different exception types in BackgroundWorker.RunWorkerCompleted event handler -


i doing little hobby project in c#, language not know well, , have stumbled upon following:

suppose have asynchronous operation implemented using backgroundworker. if there exception, event runworkercompleted raised , runworkercompletedeventargs.error non-null.

is following canonical way handle different exception types? (here exception kinds siblings wrt inheritance)

if (e.error != null) {     firstkindofexception e1 = e onekindofexception;     secondkindofexception e2 = e secondkindofexception;     ...     lastkindofexception en = e lastkindofexception;     if (e1 != null)     {         ...     }     else if (e2 != null)     {         ...     }     ...     else     {         ...     } } 

it works, but... doesn't feel right.

you use is keep each test tightly scoped:

if (e.error firstkindofexception ) {     ... } else if (e.error secondkindofexception) {     ... } 

(then re-cast if want special values exception)

to honest, though, pretty rare need handle lots of different types of exceptions. in cases, fine recover (compensate) known state , report error appropriately. prefer test errors before start action, exception exceptional.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -