Working on existing code, I recently found smt like this:
if(Config.ExceptionBehavior.DEBUG_EXCEPTIONS)
{
foo.Foo();
}
else
{
try
{
foo.Foo();
}
catch(Exception ex)
{
//whatever
}
}
I am getting rid of this – but I do see some value in the driver of this kind of code: basically the guy who wrote this wanted the thing to crash on the line the exception occurred for debug purpose. At the same time this smells awfully, because you’re replicating your code arbitrarily, which makes everything quite messy and littered.
Is there any decent why of obtaining similar behavior without shamelessly littering your code?
The only alternative I can think of is a bunch of #if DEBUG etc. but wondering if there is any app wide exception handling lib that can give me something like this.
Any pointers appreciated!
If the point of this line is to make sure your program stops on the line throwing the exception, even if you then later on catch that exception, you can configure Visual Studio to do just that, with no changes to your code.
Here’s how.
Granted, there’s no way to control which source code file, namespace, project or whatnot that this setting is for, so if the code in question is throwing “Exception” or some other exception type that might get thrown a lot, then you can’t use this solution, or… you could change that code.