新手请教:关于try/catch块测试字段的问题
为什么一般用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