Create c# exception and not throw it -
is there way better i'll have written below exception set when thrown?
try { throw new exception("blah"); } catch (exception exe) { assert.notnull(exe.stacktrace); dosomework(exe); // throw; }
the short answer : no.
the system.exception properties filled in when thrown:
by default, stack trace captured before exception object thrown. use environment.stacktrace stack trace information when no exception being thrown.
so if need exception
object in state after being thrown have no other way throw , catch it.
still not drop off main question: need exception object?
if you've got method has system.exception
input parameter , need stacktrace inside, think of these possible solutions:
- method overload optional stacktrace input parameter.
- a successor of system.exception hiding stacktrace property memorizes stacktrace when object created not thrown.
as last resort make extension method system.exception class "populates" instance of system.exception:
private void mycode() { exception exe = new exception("blah"); exe.populate(); dosomework(exe); } public static void populate(this system.exception source) { try { throw source; } catch { } }
Comments
Post a Comment