开发者

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!开发者_JAVA技巧
     }

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!


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!
    }
}


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!
    }
}


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


See if the DebuggerNonUserCodeAttribute can help you.


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!
    }
 }


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
 }  


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜