异常处理c#
本文关键字:异常处理 | 更新日期: 2023-09-27 18:19:05
我在这方面相对较新,一直在绞尽脑汁试图让我的程序正常工作,但它就是不会。我在Visual Studio 2012 c#中工作在一个表单应用程序。
我需要它在用户输入值大于0
但小于10,000
时产生明显的错误消息。当用户输入非数值时,它还必须产生一个明显的错误消息,当用户根本没有输入任何值时,它必须产生一个明显的错误消息。
到目前为止,我所编写的代码在用户输入非数字值或根本没有输入任何值时产生一个明显的错误消息,但是当用户输入的值低于或超过所需范围时,它不会触发错误消息。
这就好像编译器忽略了我为第一个异常/溢出异常编写的代码,只识别第二个也是最后一个异常的代码。我的代码没有编码错误。看来我的问题出在逻辑上。
请帮助我,如果你可以。我的代码是下面谢谢!
private void btnCalculate_Click(object sender, System.EventArgs e)
{
try
{
{
decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
decimal discountPercent = .25m;
decimal discountAmount = subtotal * discountPercent;
decimal invoiceTotal = subtotal - discountAmount;
lblDiscountPercent.Text = discountPercent.ToString("p1");
lblDiscountAmount.Text = discountAmount.ToString("c");
lblTotal.Text = invoiceTotal.ToString("c");
}
}
catch (OverflowException)
{
decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
if (subtotal <= 0)
{
MessageBox.Show("Subtotal must be greater than $0.00. ", "Error Entry");
txtSubtotal.Focus();
}
if (subtotal >= 10000)
{
MessageBox.Show("Subtotal must be less than $10000.00. ", "Error Entry");
txtSubtotal.Focus();
}
}
catch (Exception)
{
if (txtSubtotal.Text == "")
{
MessageBox.Show("Subtotal is a required field. ", "Error Entry");
}
else
{
MessageBox.Show(
"Please enter a valid Number for the subtotal field.", "Error Entry");
txtSubtotal.Focus();
}
}
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void txtSubtotal_TextChanged(object sender, EventArgs e)
{
}
}
}
我将使用KeyEvent按enter或Leave事件,首先我需要创建通用类来验证来自用户的输入是否不是字符串。条件:1 .验证输入是否不是字符串,我使用generic for general purposes类。
public class iCnF()
{
public static System.Boolean IsNumeric(System.Object Expression)
{
if (Expression == null || Expression is DateTime)
return false;
if (Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean)
return true;
try
{
if(Expression is string)
Double.Parse(Expression as string);
else
Double.Parse(Expression.ToString());
return true;
} catch {} // just dismiss errors but return false
return false;
}
}
然后我需要验证输入是否为空
private void txtSubtotal_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab) { if (txtSubtotal.Text.Length > 0) { bool sd = iCnF.IsNumeric(txtSubtotal.Text); if (sd == false) { MessageBox.Show("Subtotal must be a numeric value. ", "Error Entry"); txtSubtotal.Clear(); txtSubtotal.Focus(); } else { decimal subtotal = Convert.ToDecimal(txtSubtotal.Text); if (subtotal <= 0) { MessageBox.Show("Subtotal must be greater than $0.00. ", "Error Entry"); txtSubtotal.Focus(); } if (subtotal >= 10000) { MessageBox.Show("Subtotal must be less than $10000.00. ", "Error Entry"); txtSubtotal.Focus(); } } } else { MessageBox.Show("Subtotal must not be empty. ", "Error Entry"); txtSubtotal.Focus(); } } }
如果不为空且数值my subtotal <= 0 and subtotal>= 10000
D
正如我在评论中已经提到的,你应该考虑将你的代码移出catch块。
在这种情况下,您应该考虑创建一个简单的方法来验证您的输入并产生输出消息。
给你一个例子:
private bool IsPageValid()
{
string errorMessage = string.Empty;
bool isValid = true;
if (subtotal <= 0)
{
errorMessage+="<li>"+"<b>Subtotal must be greater than $0.00. ", "Error Entry"+ "</b><br/>";
txtSubtotal.Focus();
isValid=false;
}
}
同样在这个函数中编写其他验证子句。这将验证您的每个条件,如果失败,它将使isValid为假,从而不允许用户提交。
现在你应该在点击按钮时调用这个函数。
protected void btnSubmit_Click(object sender, EventArgs e)
{
ClearMessage();
if (IsPageValid().Equals(true))
{
// allow next action to happen.
}
}
代码应该像这样
try {
decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
try {
if(x<0 || x>10000){
throw new OverFlowException("");
}
//Do what ever you want
}
catch(Exception ex){
// catch overflow
}
}
catch(Exception ex){
// catch nonnumeric value
}