如何不打破例外? [英] How to NOT breaking on an exception?

查看:165
本文介绍了如何不打破例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  try 
{
instance.SometimesThrowAnUnavoidableException() ; //由于CustomException,Visual Studio暂停执行,我想防止这种情况。
}
catch(CustomException exc)
{
//处理异常并继续。
}

anotherObject.AlsoThrowsCustomException(); //这里我想要VS来捕获CustomException。

在另一部分代码中,我有多种情况发生,其中CustomException 是抛出。我想强制Visual Studio停止打破实例.SometimesThrowAnUnavoidableException()行,因为它掩盖了我有兴趣打破 CustomException 的其他地方的视图。



我尝试过 DebuggerNonUserCode ,但目的不同。



如何禁用Visual Studio仅在特定方法中捕获特定的异常?

解决方案

您可以使用自定义代码执行两步。


  1. 禁止自动打破 CustomException 异常。

  2. AppDomain.FirstChanceException 事件到您的应用程序。在处理程序中,如果实际异常是 CustomException ,请检查调用堆栈以查看是否确实要中断。

  3. 使用 Debugger.Break(); 导致Visual Studio停止。

以下是一些示例代码:

  private void ListenForEvents()
{
AppDomain.CurrentDomain.FirstChanceException + = HandleFirstChanceException;
}

private void HandleFirstChanceException(object sender,FirstChanceExceptionEventArgs e)
{
异常ex = e.Exception as CustomException;
if(ex == null)
return;

//选项1
if(ex.TargetSite.Name ==SomeThrowAnUnavoidableException)
return;

//选项2
if(ex.StackTrace.Contains(SomeThrowAnUnavoidableException))
return;

//检查ex如果你打这行
Debugger.Break();
}


I've got something like this:

try
{
    instance.SometimesThrowAnUnavoidableException(); // Visual Studio pauses the execution here due to the CustomException and I want to prevent that.
}
catch (CustomException exc)
{
    // Handle an exception and go on.
}

anotherObject.AlsoThrowsCustomException(); // Here I want VS to catch the CustomException.

In another part of code I have multiple occurencies of situations where CustomException is thrown. I would like to force the Visual Studio to stop breaking on instance.SometimesThrowAnUnavoidableException() line cause it obscures the view of other places where I'm interested in breaking on CustomException.

I tried DebuggerNonUserCode but it is for a different purpose.

How to disable Visual Studio from catching particular exception only in a certain method?

解决方案

You can use custom code to do this in two steps.

  1. Disable automatic breaking on the CustomException exception.
  2. Add a handler for the AppDomain.FirstChanceException event to your application. In the handler, if the actual exception is a CustomException, check the call stack to see if you actually want to break.
  3. Use the Debugger.Break(); to cause Visual Studio to stop.

Here is some example code:

private void ListenForEvents()
{
    AppDomain.CurrentDomain.FirstChanceException += HandleFirstChanceException;
}

private void HandleFirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
    Exception ex = e.Exception as CustomException;
    if (ex == null)
        return;

    // option 1
    if (ex.TargetSite.Name == "SometimesThrowAnUnavoidableException")
        return;

    // option 2
    if (ex.StackTrace.Contains("SometimesThrowAnUnavoidableException"))
        return;

    // examine ex if you hit this line
    Debugger.Break();
}

这篇关于如何不打破例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆