简体   繁体   English

如何在 c# 中抛出静默异常?

[英]How to throw silent exception in c#?

I have a property bound to ComboBox我有一个绑定到 ComboBox 的属性

    <ComboBox ItemsSource="{Binding AvailableTypes}"
            SelectedValue="{Binding Kind, Mode=TwoWay}}"/>

and in the property setter I throw an exception in some business circumstances to abort setting the property.在属性设置器中,我在某些业务情况下抛出异常以中止设置属性。

    public MyKind Kind
    {
        get { return kind; }
        set 
        {
            if (kind != value)
            {
                if (SomeRuleFailed(value))
                throw new Exception("to be ate by binding code");
                kind = value;
            }
        }
    }

It works smooth except the fact that VS2010 pops up every time I raise exception.除了每次我引发异常时都会弹出 VS2010 之外,它运行顺利。 Is there any kind of exception to raise or attribute to be set so debugger left in background?是否有任何异常要引发或设置属性,以便调试器留在后台?

You can set the kinds of exceptions that cause the debugger to break by going to Debug > Exceptions .您可以通过转到Debug > Exceptions来设置导致调试器中断的异常类型。 Untick the Thrown checkbox against Common Language Runtime Exceptions or pick your exception types individually.取消勾选Common Language Runtime ExceptionsThrow复选框或单独选择您的异常类型。

You should not be throwing an instance of an Exception class.您不应该抛出Exception class 的实例。 You should throw an instance of a class derived from Exception .您应该抛出一个派生自Exception的 class 的实例。

You can create your own exception class (and you should out of habit if you have something that's not already covered by the framework) then you can select the exceptions you want Visual Studio to break on.您可以创建自己的异常 class (如果您有一些框架尚未涵盖的内容,您应该养成习惯)然后您可以 select 您希望 Visual Studio 中断的异常。

Once you've created your exception class, go to Debug-->Exceptions... and then pick the exceptions you want to have the debugger break on.一旦你创建了你的异常 class, go 到 Debug-->Exceptions... 然后选择你想让调试器中断的异常。

Press Ctrl-Alt-E in Visual Studio to open the Exception configuration for the Debugger, and select or unselect what you need.在 Visual Studio 中按 Ctrl-Alt-E 打开 Debugger 的 Exception 配置和 select 或取消选择您需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM