views:

278

answers:

5

In my code, I currently have an exception handling setup which logs exceptions to text files. When I'm debugging the code, however, I'd rather not handle the exceptions and let execution stop rather than read the file, set a breakpoint, etc. Is there an easy way to do this using the Build and Release configurations (something like a preprocessor directive I could use to comment out some of the exception handling code)?

It turns out that there's a better solution than the original question asked for, see the first answer.

+3  A: 

Try something like this:

if ( System.Diagnostics.Debugger.IsAttached )
    System.Diagnostics.Debugger.Break();
else 
    LogException();
Joel Coehoorn
+5  A: 

C# does have preprocessor directives (e.g. if, define, etc...) that you could use for this purpose.

However, you could also modify the settings under "Debug -> Exceptions..." in Visual Studio so that the debugger breaks each time an exception is thrown (before execution goes to the catch block).

Aaron Marten
+1  A: 
#if DEBUG
// Something
#else
// Something else
#endif
John Saunders
A: 

I'd hesitate to use preprocessor directives. I'd imagine that you want something that easily applies to your whole solution and not stick preprocessor directives all over your code.

Of the previous answers, Joel's is good. However, you can accomplish the same thing by going to Debug/Exceptions and make sure that the checkboxes in the "Common Language Runtime Exceptions" row are selected. User-unhandled is usually selected by default.

opadilla