Praesagus
Praesagus

Reputation: 2094

Visual Studio ignore try catch - debug only

I think error handling is a good idea. :) When debugging it can get in the way - especially with nice user friendly messages. In VB6 I could just check a box for the compiler to ignore my error handling. I found the dialog that allows me to do something similar in VS, but it's about 10,000 check boxes instead of one - which is too many to change every time I want a production compilation.

Is there a way to set VS up so when I am in debugging mode I get one set of conditions and when I am in production I get another? ...or is there just another method to handling errors and debugging more efficiently?

Thanks

Upvotes: 5

Views: 5018

Answers (3)

Mark Brackett
Mark Brackett

Reputation: 85635

In code, I'd probably just do something like:

#if !DEBUG
    try {
#endif
        DoSomething();
#if !DEBUG
    } catch (Exception ex) {
        LogEx(ex);
        throw new FriendlyException(ex);
    }
#endif

Or. more generally and with less #if:

#if DEBUG
   public const bool DEBUG = true;
#else
   public const bool DEBUG = false;
#endif

try {
   DoSomething();
} catch (Exception ex) {
   if (DEBUG) throw;
   LogEx(ex);
   throw new FriendlyException(ex);
}

Or, general purpose (like the Exception Handling library from P&P):

bool HandleException(Exception ex) {
    return !DEBUG;
}

But, if your real problem is just the Visual Studio GUI - just use a macro.

Upvotes: 2

Robert Davis
Robert Davis

Reputation: 2265

Try the Debug Menu and look at Exceptions. You can set it to automatically break when an exception is thrown.

Upvotes: 3

Dustin Laine
Dustin Laine

Reputation: 38503

You can add this attribute to your methods:

[Conditional("DEBUG")]

You can also use #if #endif statements if you wish.

Upvotes: 1

Related Questions