pschorf
pschorf

Reputation: 529

How can I stop execution for ALL exceptions during debugging in Visual Studio?

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.

Upvotes: 3

Views: 2185

Answers (5)

CodeWarrior
CodeWarrior

Reputation: 331

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.

Upvotes: 0

John Saunders
John Saunders

Reputation: 161773

#if DEBUG
// Something
#else
// Something else
#endif

Upvotes: 1

Aaron Marten
Aaron Marten

Reputation: 6568

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).

Upvotes: 7

Joel Coehoorn
Joel Coehoorn

Reputation: 415620

Try something like this:

if ( System.Diagnostics.Debugger.IsAttached )
    System.Diagnostics.Debugger.Break();
else 
    LogException(); 

Upvotes: 4

Related Questions