首先,我运行我的应用程序,任何错误(处理与否)都会引发异常。
其次,我使用 aTypeConverter
将用户输入字符串转换为实际对象。
第三个TypeConverter
没有提供任何TryConvert
方法,所以我坚持使用异常进行验证,在这里使用这段相当难看的代码:
try
{
this._newValue = null;
#pragma Magic_SuppressBreakErrorThrown System.Exception
this._newValue = this.Converter.ConvertFromString(this._textBox.Text);
#pragma Magic_ResumeBreakErrorThrown System.Exception
this.HideInvalidNotification();
}
catch (Exception exception)
{
if (exception.InnerException is FormatException)
{
this.ShowInvalidNotification(this._textBox.Text);
}
else
{
throw;
}
}
我发现每次输入-
of-1
或其他一些无效字符时让 VS 中断执行会让人分心。我可以使用与此类似的东西,但也不是所有要转换为TryParse
方法的类型。
我希望可能有某种方法可以在try
不更改我的异常设置的情况下禁用代码部分的中断。