Friday, 15 April 2011

c# - How to include additional information in library exceptions -



c# - How to include additional information in library exceptions -

i working on library , want include additional contextual info in exceptions thrown library, including in exceptions not library specific, e.g. system.nullreferenceexception. (most probably, additional info help developers debug issues code and/or data.) edit: library processing big info structures can nested. want include additional info total path of field processed when exception thrown, e.g. myclass.field1.somearray[10].someotherfield.thisfails. (i think in case useful piece of info add together in exceptions).

solution 1. know wrapping recommended way include additional contextual info in exceptions. think has big downside: have alter type of exceptions. so, illustration outofmemoryexception wrapped in library specific exception , much more hard application handle specific exception (the developers have check type of innerexception property). that's why want avoid wrapping exceptions. (an additional consideration against wrapping library work objects implemented users, may throw specific exceptions, , not want modify type of these exceptions.)

solution 2. decided utilize exception.data dictionary include additional info in exceptions. downside of solution additional info not included in result of default implementation of exception.tostring(). (i include additional info in result of exception.tostring(), visible developers log exceptions log.write(ex.tostring());; in way dvelopers not have larn how invoke other formatting method library.)

edit: solution 3. thought of appending info @ end of exception.message field using reflection (since readolnly). there reasons why should avoid this?

are there other possible solutions problem avoid explained downsides? if there no other improve solutions, of above mutual implementation?

edit: want create clear, since library, not have much command on kind of exception logging user utilize , how much of library's documentation read. want avoid meaningless back upwards requests including much info possible in result of ex.tostring().

also, performance of import library (that might marketing, more important) so, well behaved scenarios, want avoid processing. is, if possible, want avoid checks if (data.field == null) throw new argumennullexception("somefield"); since, if there wrong data, code fail little bit later nullreferenceexception, can grab in convenient location , utilize method add together info in it.

i recommend define set of broad case, , grade finer detailed exceptions know library can throw. these exceptions prohibit library living defined expectations (within control) if exception thrown because of misuse on users part.

for example, missing null values - since can't enforce users of library follow rules lay out throw natural. throw new argumentnullexception("some message") or throw new mylibraryargumentnullexception().

in cases may want introduce set of custom exceptions throw new mylibrarycantdothisbecauseofthatexception(). ...you meaning.

if add together necessary checks ensure can produce output don't wrap else.

public someobject someprocess(anotherobject anotherobject){ if(anotherobject==null) throw argumentnullexception(); if(anotherobject.mustbepositive<=0) throw argumentexception("x must positive"); if(_somedepenedencymylibarycreates.notthere) throw new mylibrarymissingdepencyexception() nowihaveenoughinfotodomyjobwithnotry(); }

following these guidelines downwards path library should check , throw exceptions prevent completing task within locust of control. if run memory issues etc. allow fall naturally out of library because lived bargain. user should plenty info exception can tracked downwards without special decoder ring.

this leaves bugs in code such index out of range. allow them flow out naturally because wrapping , throwing somethingbadhappened exception create fixing code much harder.

now there caveats this...such database exceptions or exceptions in business type code reveal sensitive internals of system. in these situations might forced wrap , or check specific business class exception or database exception, logging original, , returning somthingbadhappened exception guid or reference exception logged.

another border case web service libraries may rely on specific http response code. exception shielding.

c# .net design exception-handling

No comments:

Post a Comment