验证处理一般异常

时间:2014-04-09 18:39:43

标签: .net wpf validation

验证是处理一般例外的 这不是我想要的行为 如果我的代码抛出错误(如除以零),我不想要验证来处理它

有没有办法对此进行编码?

按钮会使程序崩溃 但300,400和500我不会崩溃程序
(有趣的是安全会破坏程序而不是我的问题)

<Window x:Class="ValidationExeption.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource self}}"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Path=Error, Mode=OneWay}" />
            <TextBox Text="{Binding Path=I, ValidatesOnDataErrors=False, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}" />
            <Button Content="Exceptionn" Click="Button_Click" />
        </StackPanel>
    </Grid>
</Window>

using System.ComponentModel;
namespace ValidationExeption
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        public MainWindow()
        {
            InitializeComponent();
        }
        private int i = 0;
        public int  I
        {
            get { return i; }
            set
            {
                Error = "none";
                if (value < 0)
                {

                    Error = "ArgumentOutOfRangeException";
                    throw new ArgumentOutOfRangeException("");
                }
                else if (value == 100)
                {
                    Error = "System.Security.SecurityException";
                    i = -100;
                    NotifyPropertyChanged("I");
                    throw new System.Security.SecurityException();
                }
                else if (value == 200)
                {
                    Error = "ArgumentException";
                    i = -200;
                    NotifyPropertyChanged("I");
                    throw new ArgumentException("Security Violation");
                }
                else if (value == 300)
                {
                    Error = "Exception";
                    i = -300;
                    NotifyPropertyChanged("I");
                    throw new Exception();
                }
                else if (value == 400)
                {
                    Error = "I divide by zero";
                    i = -400;
                    int j = 0;
                    int k = i / j;  // this does not crash the app
                    NotifyPropertyChanged("I");
                }
                else if (value == 500)
                {
                    Error = "DivideByZero";
                    i = -500;
                    //int j = 0;
                    int k = DivideByZero(i);  // this does not crash the app
                    NotifyPropertyChanged("I");
                }
                else
                {
                    i = value;
                }
                NotifyPropertyChanged("I");
            }
        }
        private Int32 DivideByZero(Int32 i)
        {
            Int32 j = 0;
            return i / j;
        }
        private string error = "none";
        public string Error
        {
            get { return error; }
            set 
            {
                error = value;
                NotifyPropertyChanged("Error");
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DevideByZero(12);   // this does crash the app
            //throw new Exception();
        }
    }
}

0 个答案:

没有答案