c# - Catch Exception with Condition -


note: happy tell exception filters in c# 6.0 language.

this thought experiment, i'm interested in opinion: make sense you? know whether similar has been proposed c# programming language? wouldn't know send such proposal...

the idea introducing syntax elements catch exception if fulfills condition.

one use case example when working com interop: throws comexception. actual distinguishing error code contained in message.

so (proposal 1):

try {     ... } catch (comexception ex ex.message.contains("0x800706ba")) {     // rpc server unavailable } catch (comexception ex ex.message.contains("0x80010001")) {     // call rejected callee } 

which translates to:

try {     ... } catch (comexception ex) {     if (ex.message.contains("0x800706ba"))     {         // rpc server unavailable     }     else if (ex.message.contains("0x80010001"))     {         // call rejected callee     }     else     {         throw;     } } 

similar cases are: soapexception, xmlexception...


another scenario when exceptions wrapped inner exceptions within general exception, , catching logic should depend on inner exception.

say have api wraps exceptions this: catch (numberformatexception ex) { throw new businessexception(ex) }.

what (proposal 2a):

try {     ... } catch (inner numberformatexception nfex) {     ... } 

which translates to:

catch (exception ex ex.innerexception numberformatexception) {     numberformatexception nfex = ex.innerexception;     ... } 

or (2b):

catch (businessexception bex inner numberformatexception nfex) {     ... } 

which translates to:

catch (businessexception bex bex.innerexception numberformatexception) {     numberformatexception nfex = bex.innerexception;     ... } 

in this case (originally java) (2c):

catch (remoteaccessexception raex inner inner myexception mex) {     ... } 

according try-catch c# reference visual studio 2015 rc implemented:

catch (argumentexception e) when (e.paramname == "…") { } 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -