如何在 C# 中抛出静默异常?
我有一个绑定到 ComboBox 的属性
<ComboBox ItemsSource="{Binding AvailableTypes}"
SelectedValue="{Binding Kind, Mode=TwoWay}}"/>
,在属性设置器中,我在某些业务情况下抛出异常以中止设置该属性。
public MyKind Kind
{
get { return kind; }
set
{
if (kind != value)
{
if (SomeRuleFailed(value))
throw new Exception("to be ate by binding code");
kind = value;
}
}
}
除了每次我提出异常时 VS2010 都会弹出之外,它运行顺利。是否有任何类型的异常需要引发或属性需要设置,以便调试器留在后台?
I have a property bound to 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. Is there any kind of exception to raise or attribute to be set so debugger left in background?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该抛出 Exception 类的实例。您应该抛出一个从
Exception
派生的类的实例。您可以创建自己的异常类(如果您有框架尚未涵盖的内容,您应该出于习惯),然后您可以选择您希望 Visual Studio 中断的异常。
创建异常类后,转到“调试”-->“异常...”,然后选择要让调试器中断的异常。
You should not be throwing an instance of an
Exception
class. You should throw an instance of a class derived fromException
.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.
Once you've created your exception class, go to Debug-->Exceptions... and then pick the exceptions you want to have the debugger break on.
您可以通过转到调试>来设置导致调试器中断的异常类型。例外。取消选中公共语言运行时异常的抛出复选框,或单独选择异常类型。
You can set the kinds of exceptions that cause the debugger to break by going to Debug > Exceptions. Untick the Thrown checkbox against Common Language Runtime Exceptions or pick your exception types individually.
在 Visual Studio 中按 Ctrl-Alt-E 打开调试器的异常配置,然后选择或取消选择您需要的内容。
Press Ctrl-Alt-E in Visual Studio to open the Exception configuration for the Debugger, and select or unselect what you need.