Do all the things like ++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatarSign Up
Voxera6630102dDid you mean stack trace?
And with such error handling, yes, stack trace loose a lot of value.
But in well structured code stack traces are often very helpful in c#.
MrCSharp5674102d@galileopy Here's a tip:
If you need to "re-throw" an exception but from a different code block and at a different time, use this
this will throw the same exception and with proper stack trace reflecting where the exception was originally thrown and where it was re-thrown too.
Voxera6630101d@MrCSharp better yet, do not rethrow unless you add some useful information, like local state to help trach the cause, but always include the original error.
I do this sometimes in methods that are called multiple times with different data and where stacktrace are useless anyway as there only exist one path and the state is the important part.
MrCSharp5674101d@Voxera I'd take that one step further and say don't even put a try catch block unless you Will do something with the fact the code has thrown an exception.
Catching and then rethrowing without additional processing of the error or an attempt to correct it is meaningless.
As for my example of rethrowing using that utility, it can be helpful in a few situations and it heavily depends on how the code is structured. Like everything else in coding the answer is: it depends.
Voxera6630100d@MrCSharp in my case we use it when the original error comes from code nit under our control and where we for example need the input to the method that are throwing an exception for tracking the error.
Fo example where we have failed to recreate the error and need more.
Also in my script engine as stack trace there is often 100 levels deep recursing through the same set of methods traversing the ast tree.
In that case current state or input is the most import information to identify which instruction it was processing with what data, not the line or stack trace ;)
But thats also a very odd edge case most will never encounter.
Your Job Suck?
Take a quick quiz from Triplebyte to skip the job search hassles and jump to final interviews at hot tech firms
Get a Better Job