greggorob64
greggorob64

Reputation: 2557

Is it possible to completely ignore a catch block when debugging?

I'm working within WinForms (.net 3.5), and have the following line of code:

     HitTestResult result;
     try
     {
        result = this.HitTest( e.X, e.Y, ChartElementType.DataPoint);
     }
     catch(Exception e)
     {
        //This happens, we don't care!
     }

I have no control over whether HitTest throws an exception, but if it does, I absolutely do not care.

Is it possible to disable my IDE from halting at this SPECIFIC catch block? I understand I can disable the System.FormatException it may throw (from the Debug->Exceptions menu, but that's kind of overkill.

Thanks!

Upvotes: 9

Views: 2282

Answers (7)

Igby Largeman
Igby Largeman

Reputation: 16747

You can place the try/catch block in its own method and decorate that method with any of the following attributes:

DebuggerStepThrough - causes the debugger to step over the method

DebuggerHidden - hides the method from the debugger (won't even allow breakpoints)

DebuggerNonUserCode - a combination of the previous two

Upvotes: 6

Keith
Keith

Reputation: 21244

I understand you want to break on all exceptions except this one but VS doesn't offer this option.

I would recommend rewriting the HitTest() method so that it handles failures gracefully instead of allowing exceptions to be thrown, for example by validating the input parameters.

Correction: 0A0D's answer shows that this is possible and that will work best with API calls or other code you don't have control over. But when you do, you should consider rewriting it to handle errors gracefully.

Upvotes: 0

BrokenGlass
BrokenGlass

Reputation: 160852

If you factor out your code into a separate method you can decorate it with DebuggerStepThrough and the IDE will not halt:

[DebuggerStepThrough]
public void SomeMethod()
{
    HitTestResult result;
    try
    {
        result = this.HitTest(e.X, e.Y, ChartElementType.DataPoint);
    }
    catch (Exception e)
    {
        //This happens, we don't care!
    }
}

Upvotes: 10

user195488
user195488

Reputation:

You can use the DebbugerStepThrough attribute to skip over that line. From MSDN:

Instructs the debugger to step through the code instead of stepping into the code.

For example:

[DebuggerStepThrough]
public void MyMethod()
{
    HitTestResult result;
    try
    {
        result = this.HitTest(e.X, e.Y, ChartElementType.DataPoint);
    }
    catch (Exception e)
    {
        //This happens, we don't care!
    }
}

Upvotes: 16

Chuck Savage
Chuck Savage

Reputation: 11945

Try,

 HitTestResult result;
 try
 {
    result = this.HitTest( e.X, e.Y, ChartElementType.DataPoint);
 }
 catch(Exception e)
 {       
    if(!Debugger.IsAttached)
    {
        //This happens, we don't care!
    }
 }

Upvotes: 1

Eben Roux
Eben Roux

Reputation: 13246

See if the DebuggerNonUserCodeAttribute can help you.

Upvotes: 2

InBetween
InBetween

Reputation: 32740

Why not do the following?

 HitTestResult result;
 try
 {
    result = this.HitTest( e.X, e.Y, ChartElementType.DataPoint);
 }
 catch(Exception e)
 {
 #if !DEBUG
    //This happens, we don't care!
 #endif
 }  

Upvotes: 0

Related Questions