.net 由验证处理的常规异常
验证正在处理一般异常 这不是我想要的行为 如果我的代码抛出像被零除这样的错误,我不想让验证来处理它 有没有一种方法可以编码 按钮将使程序崩溃 但是300、400和500,因为我不会使程序崩溃 有趣的是,安全性会使程序崩溃,但不是我的问题.net 由验证处理的常规异常,.net,wpf,validation,.net,Wpf,Validation,验证正在处理一般异常 这不是我想要的行为 如果我的代码抛出像被零除这样的错误,我不想让验证来处理它 有没有一种方法可以编码 按钮将使程序崩溃 但是300、400和500,因为我不会使程序崩溃 有趣的是,安全性会使程序崩溃,但不是我的问题 <Window x:Class="ValidationExeption.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<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();
}
}
}
在我的例子中,所有的例子——300、400和500都会引发异常和程序崩溃。此外,在您的示例输入错误中:DivideByZero和DeviceByzero12。我刚才已经验证并同意@Anatoliy Nikolaev@AnatoliyNikolaev哇,我是.NET 3.5。我将尝试其他配置。作为补充:我在.NET 4.0和Windows 7下运行您的示例。@AnatoliyNikolaev我之前的评论不正确。这是4.0和VS 2010。我用VS 2013复制到另一个框中,得到了我在4.0、4.5和4.51的问题中描述的行为