vb.net - When catching a general exception, how can I determine the original exception type? -
when catching exception in .net, can have many type-specific exception blocks needed. try have @ least 1 "general" exception catch block. there way type of "real" exception thrown caught generic exception handler, perhaps using reflection?
for example, if have
catch ex system.servicemodel.faultexception(of invaliduser) processmoduleloadexception(me, ex) catch ex system.servicemodel.faultexception(of sqlexceptions) processmoduleloadexception(me, ex) catch ex system.servicemodel.faultexception(of datanullreference) processmoduleloadexception(me, ex) catch ex system.servicemodel.faultexception processmoduleloadexception(me, ex) catch ex exception processmoduleloadexception(me, ex)
(i have broken them out debugging, though same thing each exception).
in short, in "catch ex system.servicemodel.faultexception" want examine "ex" , base "real" type of exception, either type (mostly debugging can add catch block) or string (for logging).
but, inside catch block, "ex" has been cast it's parent class, original properties , information original exception seem lost.
suggestions?
even though exception
has been cast parent class, can still call gettype
obtain underlying concrete type (pardon c#):
try { // stuff } catch(exception ex) { type exceptiontype = ex.gettype(); }
if diagnostic purposes, can log ex.tostring()
, includes underlying type default (in addition stack trace, etc.) this:
system.argumentexception: lastname @ tests.program.main(string[] args) in ...\program.cs:line 22
Comments
Post a Comment