简体   繁体   中英

How to throw silent exception in c#?

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?

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.

You should not be throwing an instance of an Exception class. You should throw an instance of a class derived from Exception .

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.

Press Ctrl-Alt-E in Visual Studio to open the Exception configuration for the Debugger, and select or unselect what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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