Reputation: 51
I am fairly new to C#. Although throwing the exceptions for the other conditions work fine, the string (or lack of entry would be a better term to use) does not work. It instead goes straight to catch (FormatException) message.
I know that I could put the same statement if (txtSubtotal.Text == "") in the aforementioned catch statement, and it would work fine, but I am really curious as to why I cannot do this by throwing a new exception.
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");
}
}
Upvotes: 0
Views: 83
Reputation: 1996
This behavior is due to your string being null or empty and the code trying to parse a null or empty string to a Decimal.
It would be better to check the string before trying to parse it. Also you can check the contents of a string easily with the string.IsNullOrWhitespace method from the string class.
Like this:
try
{
if (string.IsNullOrWhitespace(txtSubtotal.Text))
throw new Exception("Subtotal is a Required Field.");
decimal subtotal = Decimal.Parse(txtSubtotal.Text);
//rest of your code.
With the code like this and the string is null or white space, the basic Exception will be thrown...but if the string is a NOT null or white space AND it is not a valid number format either, then the code will throw the FormatException.
Upvotes: 0
Reputation: 633
Couple things here, first I think there is a logic issue confusing your expected result. Take this excerpt...
decimal subtotal = Decimal.Parse(txtSubtotal.Text);
{
if (txtSubtotal.Text == "")
throw new Exception("Subtotal is a Required Field.");
...
Decimal.Parse is always going to throw a FormatException on an empty string, or any other value where it can't convert to a decimal. This means the next if condition never gets executed. And if it did get executed, it'll never be true because you just proved it otherwise with a successful Decimal.Parse.
Upvotes: 0
Reputation: 29026
The method Decimal.Parse()
will throws FormatExceptions if the parameter(txtSubtotal.Text
) to the method is not convertible. To avoid this you can You trust Decimal.TryParse()
for doing the same, without any try..catch
try the following code:
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");
}
Upvotes: 2