Try/Catch Statements
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Try/Catch Statements

Hybrid View

  1. #1
    Join Date
    Mar 2013
    Posts
    1

    Try/Catch Statements

    Hello there! I am fairly new to C# and I'm having a problem coding a specific assignment. I was wondering if anyone could provide a few tips on what I'm doing wrong. I'm trying to have it catch 3 different things. That there is a decimal value, that there actually is something inserted, and that the number is within range of 0 and 1000. All of these need to display a different message box also. Down below is so far what I've done. I'm able to do the valid range but it's still calculating the numbers when I click calculate. Aslo when there is no value entered it brings up the "Please enter a valid subtotal" message box instead of the "Requires Subtotal" box. Thanks!
    Code:
    private void btnCalculate_Click(object sender, EventArgs e)
            {
                try
                {
                    
                    
                    decimal subtotal = Decimal.Parse(txtSubtotal.Text);
                    if (subtotal < 0 || subtotal > 1000)
                    {
                        MessageBox.Show("Please enter a valid range");
                        txtSubtotal.Focus();
                                        }
                    if (txtSubtotal.Text == "")
                    {
                        MessageBox.Show("Requires Subtotal");
                        return false;
                    }
                    
                               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();
    
                    Convert.ToDecimal(txtSubtotal.Text);      
                }
                catch (FormatException)
                {
                    MessageBox.Show("Please enter a valid subtotal");
                }
               
            }
    Last edited by DataMiser; March 13th, 2013 at 01:43 AM. Reason: added code tags for readability

  2. #2
    Join Date
    Mar 2013
    Posts
    6

    Re: Try/Catch Statements

    It works just as you designed it.

    This line
    Code:
    decimal subtotal = Decimal.Parse(txtSubtotal.Text);
    Throws a FormatException if the value is unabled to be parsed. And the code in the catch statement i executed.
    Run the code in the debuger and you see where the exception occurs.

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Try/Catch Statements

    Let's think about the logic of your program, in comments:

    Code:
                    //convert the value the user typed into a decimal
                    decimal subtotal = Decimal.Parse(txtSubtotal.Text);
    
                    //check it's within range
                    if (subtotal < 0 || subtotal > 1000)
                    {
                        MessageBox.Show("Please enter a valid range");
                        txtSubtotal.Focus();
                    }
    
                    //check if the user typed anything or not
                    if (txtSubtotal.Text == "")
                    {
                        MessageBox.Show("Requires Subtotal");
                        return false;
                    }
                    
                               decimal discountPercent = .25m;

    Can you see a problem?


    Here's a clue, I've stripped the code, leaving just 2 relevant comments:

    Code:
     //first, take the value the user typed, and convert it to a decimal
     //then, check if the user typed something
    What strikes you about this logic?


    -

    When you start off in programing, write the commentfirst in plain english and embody the logic, then turn the comments into code. You end up with a program that works because you first wrote it in the language that you think in, and you have a beautifully commented code at the end
    Good result!
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Mar 2013
    Posts
    1

    Re: Try/Catch Statements

    cool...

    Please i know it's odd to ask this here how do i put signatures here for my site with a link like this : Jobs in Nigeria today

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center