2016-07-05 5 views
0

私はC#をかなり新しくしています。他の条件の例外をスローすることはうまくいきますが、文字列(またはエントリの欠落は使用する方が良いでしょう)が機能しません。代わりにcatch(FormatException)メッセージにまっすぐに進みます。文字列がスローされない

上記のcatchステートメントで(txtSubtotal.Text == "")if()と同じステートメントを入れることができることは知っていますが、うまくいくでしょうが、なぜ私がこれを行うことができないのか不思議です新しい例外がスローされます。メソッドへのパラメータ(txtSubtotal.Text)は変換できない場合

private void btnCalculate_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     decimal subtotal = Decimal.Parse(txtSubtotal.Text); 
     { 
      if (txtSubtotal.Text == "") 
       throw new Exception("Subtotal is a Required Field."); 

      if (subtotal <= 0) 
       throw new Exception("Subtotal must be greater than 0"); 

      if (subtotal >= 10000) 
       throw new Exception("Subtotal must be less than 10000"); 
     } 

     decimal discountPercent = .25m; 
     decimal discountAmount = subtotal * discountPercent; 
     decimal invoiceTotal = subtotal - discountAmount; 

     discountAmount = Math.Round(discountAmount, 2); 
     invoiceTotal = Math.Round(invoiceTotal, 2); 

     txtDiscountPercent.Text = discountPercent.ToString("p1"); 
     txtDiscountAmount.Text = discountAmount.ToString(); 
     txtTotal.Text = invoiceTotal.ToString(); 

     txtSubtotal.Focus(); 
    } 
    catch (FormatException) 
    { 
     MessageBox.Show("Please enter a valid number for the Subtotal field.", "Entry Error"); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message + "\n" + "\n", "Entry Error"); 
    } 
} 
+1

論理フローを制御するために例外を使用しないでください。これは非常に悪い習慣であり、新しいプログラマとして、あなたはそれを避けようとするべきです。 –

答えて

2

方法Decimal.Parse()意志FormatExceptionsをスロー。あなたはあなたが同じことを行うためDecimal.TryParse()を信頼することができ、これを回避するために、任意のtry..catch次のコードを試してくださいなし:

decimal subtotal; 
if (Decimal.TryParse(txtSubtotal.Text, out subtotal)) 
{ 
    if (subtotal <= 0) 
     MessageBox.Show("Subtotal must be greater than 0"); 
    else if (subtotal >= 10000) 
     MessageBox.Show("Subtotal must be less than 10000"); 
    else 
    { 
    // Process your code here 
    } 
} 
else 
{ 
    MessageBox.Show("Invalid Input! Subtotal Expecting a Decimal value"); 
} 
+1

'TryParse'コールで' out subtotal'が見つかりません – Blorgbeard

+0

@Blorgbeard:ありがとうございます、私は答えを更新しました –

0

ここではカップルの事を、最初に私はあなたの期待される結果を混乱ロジックの問題があると思います。この抜粋を取る...

decimal subtotal = Decimal.Parse(txtSubtotal.Text); 
{ 
    if (txtSubtotal.Text == "") 
    throw new Exception("Subtotal is a Required Field."); 
... 

Decimal.Parseは常に空の文字列、またはそれが小数に変換することができない他の値にFormatExceptionをスローする予定です。これは、次のif条件が決して実行されないことを意味します。そして、それが実行された場合、成功したDecimal.Parseでそれがそうでなければそれを証明したので、それは決して真実ではありません。

0

この現象は、文字列がnullまたは空で、コードがnullまたは空の文字列をDecimalに解析しようとしていることが原因です。

解析する前に文字列をチェックする方がよいでしょう。また、文字列クラスのstring.IsNullOrWhitespaceメソッドを使用して、文字列の内容を簡単にチェックすることもできます。このよう

:このようなコードと文字列で

try 
{ 
    if (string.IsNullOrWhitespace(txtSubtotal.Text)) 
     throw new Exception("Subtotal is a Required Field."); 

    decimal subtotal = Decimal.Parse(txtSubtotal.Text); 

    //rest of your code. 

がNULLまたは空白で、基本的な例外がスローされます...が、文字列はスペースとそれnullまたは白されていない場合有効な数値形式でもない場合、コードはFormatExceptionをスローします。

関連する問題