为什么一般用try/catch嵌套的方式来逐个检测字段的合法性,而不是顺序的去检测,这两种方式有区别吗?嵌套方式检测:
try
{
intQuantity = int.Parse(quantityTextBox.Text);
try
{
decPrice = decimal.Parse(priceTextBox.Text);
}
catch
{
MessageBox.Show("Price must be numeric");
priceTextBox.SelectAll();
priceTextBox.Focus();
} }
catch
{
MessageBox.Show("Quantity must be numeric");
quantityTextBox.SelectAll();
quantityTextBox.Focus();
}
顺序检测:
try
{
intQuantity = int.Parse(quantityTextBox.Text);
}
catch
{
MessageBox.Show("Quantity must be numeric");
quantityTextBox.SelectAll();
quantityTextBox.Focus();
}
try
{
decPrice = decimal.Parse(priceTextBox.Text);
}
catch
{
MessageBox.Show("Price must be numeric");
priceTextBox.SelectAll();
priceTextBox.Focus();
}
try
{
intQuantity = int.Parse(quantityTextBox.Text);
try
{
decPrice = decimal.Parse(priceTextBox.Text);
}
catch
{
MessageBox.Show("Price must be numeric");
priceTextBox.SelectAll();
priceTextBox.Focus();
} }
catch
{
MessageBox.Show("Quantity must be numeric");
quantityTextBox.SelectAll();
quantityTextBox.Focus();
}
顺序检测:
try
{
intQuantity = int.Parse(quantityTextBox.Text);
}
catch
{
MessageBox.Show("Quantity must be numeric");
quantityTextBox.SelectAll();
quantityTextBox.Focus();
}
try
{
decPrice = decimal.Parse(priceTextBox.Text);
}
catch
{
MessageBox.Show("Price must be numeric");
priceTextBox.SelectAll();
priceTextBox.Focus();
}
解决方案 »
- bmp图像写入到现有的avi文件
- 关于媒体播放的
- 求解:翻译一段vb代码
- 到http://www.preemptive.com/support/index.html这里注册,英语水平太差,请帮忙注册一下吧,谢谢!
- 什么叫泡沫排序算法
- devexpress treelist 节点
- 请问c#里面写留言板一般用什么样的方法?
- 主窗体Show了好几个窗体,不是Mdi窗体,如何隐藏所有这些Show出来的窗体?高分求解!
- C#用户控件问题
- 怎么写俄罗斯方块,学习了多年的java,写不出俄罗斯方块,感觉自己还不会写程序。最多出400分
- 异常详细信息: System.InvalidOperationException: 数据源的类型无效。它必须是 IListSource、IEnumerable 或 IDataSource。
- c# 2003 datagrid如何获取当前格子列头文本?
出错时就直接到catch {}了,不再执行
decPrice = decimal.Parse(priceTextBox.Text);
顺序检测:intQuantity = int.Parse(quantityTextBox.Text);
出错时就catch {}了,然后还要执行
decPrice = decimal.Parse(priceTextBox.Text);
private bool IsNumumber(string str)
{
return System.Text.RegularExpressions.Regex.IsMatch(str, NUMBER_PATTERNS, System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}
调用:
this.lblResult.Text = this.IsNumumber(this.txtSrc.Text).ToString();