How to nest try/catch blocks

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1744 Views - Last Post: 28 October 2014 - 04:12 PM Rate Topic: -----

#1 pavsingh   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 27-October 14

How to nest try/catch blocks

Posted 27 October 2014 - 08:13 PM

a) When the user selects/changes a system, the weight and height input boxes are enabled and the appropriate units appear with the weight and height input boxes. All input and output fields are cleared.

B) If the user clicks the Calculate button without selecting any system, an appropriate error message must come up.
c) The user should enter his/her weight in the required units. The weight and height values must be positive numbers. These fields cannot be left blank or contain non numeric data or have out-of-bounds value. In case of any these input errors, separate appropriate error messages should appear for the weight and height inputs. Use two nested try/catch blocks or the like in functionality and decision structures to achieve this. Perform the calculations ONLY IF ALL DATA IS COMPLETE AND VALID

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class formBMI : Form
    {
        
private  object selectedRadioButton;public formBMI()
        {
            InitializeComponent();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            wTextBox.Clear();
            hTextBox.Clear();
            // Clear the output
            BodyTextBox.Clear();
            ResultTextBox.Clear();

            // clear the radio button with clear button
            ImpRadioButton.Checked = false;
            MetricRadioButton.Checked = false;



        }

        private void button1_Click(object sender, EventArgs e)
        {
            //calculate the amount due, taxes, for an item and display it.
            decimal wDecimal, hDecimal,  bmDecimal, resultDecimal;
            //check if carSalePrice provided is a whole number
            try
            {
                wDecimal = decimal.Parse(wTextBox.Text);
                if (wDecimal > 0)
                {
                    //check which Radio Button is selected
                    if (selectedRadioButton != null)
                    {
                        switch (selectedRadioButton.Name)
                        {
                            case "ImpRadioButton":
                                wDecimal = ImpRadioButton;
                                break;
                            case "MetricRadioButton":
                                saleDecimal = MetricRadioButton;
                                break;
                            
                        }
                        
                    //  {
                    //calculate the amounts
                    resultDecimal += wDecimal + hDecimal;
                    
                    // Display amounts in labels using string.
                    hTextBox.Text = resultDecimal.ToString("c");
                                      
                }

                      //no value is selected, Error message
                else
                {
                    MessageBox.Show("Please select an option",
                        "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                
                       

                // catch for format expection
            catch (FormatException mathFE)
            {
                if (hTextBox.Text == String.Empty)
                {
                    MessageBox.Show(" The given value >0 MUST be provided, cannot leave it blank",
                            "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    hTextBox.Focus();

                }
                else
                {
                    MessageBox.Show("The given value MUST be a positive whole number",
                           "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    hTextBox.SelectAll();
                    hTextBox.Focus();


                }
            }
            catch (OverflowException mathOE)
            {
                MessageBox.Show("The given value out of range...TOO BIG",
                          "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 hTextBox.SelectAll();
                 hTextBox.Focus();
            }
            catch (Exception eex)
            {
                MessageBox.Show("Error:" + eex.Message,
                          "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);


             }

        }
    }
}



I m using try catch. and not expert in this try catch. in calculate button i an looking When the user selects/changes a system, the weight and height input boxes are enabled and the appropriate units appear with the weight and height input boxes.
The user should enter his/her weight in the required units. The weight and height values must be positive numbers. These fields cannot be left blank or contain non numeric data or have out-of-bounds value. In case of any these input errors, separate appropriate error messages should appear for the weight and height inputs. Use two nested try/catch blocks or the like in functionality and decision structures to achieve this.

This post has been edited by tlhIn`toq: 28 October 2014 - 03:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to nest try/catch blocks

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14409
  • View blog
  • Posts: 57,772
  • Joined: 12-June 08

Re: How to nest try/catch blocks

Posted 27 October 2014 - 08:20 PM

What is your question?
Was This Post Helpful? 0
  • +
  • -

#3 pavsingh   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 27-October 14

Re: How to nest try/catch blocks

Posted 27 October 2014 - 08:23 PM

how to use two nested try/catch blocks? According to attached picture.

i do not know i attachment a picture in my comments, you able to see or not?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6441
  • View blog
  • Posts: 22,070
  • Joined: 05-May 12

Re: How to nest try/catch blocks

Posted 27 October 2014 - 08:26 PM

First of all, what attached picture?

Second, of all, your teacher is teaching you the wrong thing in telling you to use exceptions as flow control for this type of invalid inputs. You could simply use the TryParse() methods of the appropriate type and not even need any try-catch.
Was This Post Helpful? 0
  • +
  • -

#5 pavsingh   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 27-October 14

Re: How to nest try/catch blocks

Posted 27 October 2014 - 08:31 PM

I want to pass out but teacher expecting Use "two nested try/catch blocks"

These are the function requirements.
Functionality This program calculates and displays the Body Mass Index and information based on the BMI, as shown in the above table in the problem definition. Write efficient, non-redundant code to achieve this functionality .
a) When the user selects/changes a system, the weight and height input boxes are enabled and the appropriate units appear with the weight and height input boxes. All input and output fields are cleared.
B) If the user clicks the Calculate button without selecting any system, an appropriate error message must come up.
c) The user should enter his/her weight in the required units. The weight and height values must be positive numbers. These fields cannot be left blank or contain non numeric data or have out-of-bounds value. In case of any these input errors, separate appropriate error messages should appear for the weight and height inputs. Use two nested try/catch blocks or the like in functionality and decision structures to achieve this. Perform the calculations ONLY IF ALL DATA IS COMPLETE AND VALID.
d) The Calculate button initiates the BMI calculations if all input is complete and displays the results. Please ensure that there is no redundant code. Format the BMI to 1 place of decimal.

I am using try to use just single first but not getting success because i am learning try catch and kinda beginner in this C#.

help will be appreciated.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 pavsingh   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 27-October 14

Re: How to nest try/catch blocks

Posted 27 October 2014 - 08:51 PM

and what could be formula for calculate the BMI?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14409
  • View blog
  • Posts: 57,772
  • Joined: 12-June 08

Re: How to nest try/catch blocks

Posted 27 October 2014 - 08:56 PM

I am not a large fan of this, but try searching "bmi formula".
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6441
  • View blog
  • Posts: 22,070
  • Joined: 05-May 12

Re: How to nest try/catch blocks

Posted 27 October 2014 - 08:58 PM

View Postpavsingh, on 27 October 2014 - 11:31 PM, said:

I want to pass out but teacher expecting Use "two nested try/catch blocks"

:

Use two nested try/catch blocks or the like in functionality and decision structures to achieve this.


I recommend the other option that I highlighted in bold above.
Was This Post Helpful? 0
  • +
  • -

#9 Yips   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 64
  • Joined: 11-October 14

Re: How to nest try/catch blocks

Posted 27 October 2014 - 08:59 PM

View Postpavsingh, on 27 October 2014 - 11:51 PM, said:

and what could be formula for calculate the BMI?


BMI English Formula: (Weight 'in pounds' / (height 'in inches' * height 'in inches')) * 703

BMI Metric Formula: (Weight 'in kg' / (height 'in meters' * height 'in meters'))
Was This Post Helpful? 0
  • +
  • -

#10 pavsingh   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 27-October 14

Re: How to nest try/catch blocks

Posted 27 October 2014 - 09:05 PM

private void button1_Click(object sender, EventArgs e)
        {
            //calculate the amount due, taxes, for an item and display it.
            decimal wDecimal, hDecimal,  bmDecimal, resultDecimal;
            //check if carSalePrice provided is a whole number
            try
            {
                wDecimal = decimal.Parse(wTextBox.Text);
                if (wDecimal > 0)
                {
                    //check which Radio Button is selected
                    if (selectedRadioButton != null)
                    {
                        switch (selectedRadioButton.Name)
                        {
                            case "ImpRadioButton":
                                wDecimal = ImpRadioButton;
                                break;
                            case "MetricRadioButton":
                                saleDecimal = MetricRadioButton;
                                break;
                            
                        }
                       try
                       {
                           hDecimal = decimal.Parse(hTextBox.Text);
                           if(hDecimal>0)
                               if (selectedRadioButton != null)
                    {
                        switch (selectedRadioButton.Name)
                        {
                            case "ImpRadioButton":
                                wDecimal = ImpRadioButton;
                                break;
                            case "MetricRadioButton":
                                saleDecimal = MetricRadioButton;
                                break;
                         }
                        
                    //  
                    //calculate the amounts
                    resultDecimal =(Weight 'in pounds' / (height 'in inches' * height 'in inches')) * 703

                                   (Weight 'in kg' / (height 'in meters' * height 'in meters')) ;
                    
                    // Display amounts in labels using string.
                    hTextBox.Text = resultDecimal.ToString("c");
                    wTextBox.Text = resultDecimal.ToString("c");
                                      
                }

                      //no value is selected, Error message
                else
                {
                    MessageBox.Show("Please select an option",
                        "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                
                       

                // catch for format expection
                catch (FormatException mathFE)
            {
                if (hTextBox.Text == String.Empty)
                {
                    MessageBox.Show(" The given value >0 MUST be provided, cannot leave it blank",
                            "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    hTextBox.Focus();

                }
                else
                {
                    MessageBox.Show("The given value MUST be a positive whole number",
                           "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    hTextBox.SelectAll();
                    hTextBox.Focus();


                }
            }
            catch (OverflowException mathOE)
            {
                MessageBox.Show("The given value out of range...TOO BIG",
                          "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 hTextBox.SelectAll();
                 hTextBox.Focus();
            }
            catch (Exception eex)
            {
                MessageBox.Show("Error:" + eex.Message,
                          "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);


             }

        }
    }
}


no working
Was This Post Helpful? 0
  • +
  • -

#11 Yips   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 64
  • Joined: 11-October 14

Re: How to nest try/catch blocks

Posted 27 October 2014 - 09:07 PM

I didn't input your variables for the formulas, that's just the accepted formulas for the English and metric system. You need to input your variables for the different functions for the formula. Anything in ' ' is the measurement for that value.

This post has been edited by Yips: 27 October 2014 - 09:08 PM

Was This Post Helpful? 0
  • +
  • -

#12 pavsingh   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 27-October 14

Re: How to nest try/catch blocks

Posted 27 October 2014 - 09:11 PM

I'm upset. i going to lose the assignment. i can't fix this requirments. my brain is not too sharp. :nottalkingtoyou: :nottalkingtoyou: :nottalkingtoyou: :nottalkingtoyou: :nottalkingtoyou:
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to nest try/catch blocks

Posted 28 October 2014 - 03:51 AM

Let's get back on track. Telling him his teacher is a dork and a bad teacher doesn't change the assignment requirements. He's not going to tell his teacher "Some guy on the internet says you don't know what you're doing"

View Postmodi123_1, on 27 October 2014 - 09:20 PM, said:

What is your question?


View Postpavsingh, on 27 October 2014 - 09:23 PM, said:

how to use two nested try/catch blocks?


You put all of one try/catch inside the try block of the outer try/catch

try
{
   try
   {
     // do some work here
   }
   catch
   {
     // Catch inner error here
   }
}
catch
{
   // Catch outer error here
}


It would help you a LOT if you would take the time to clean up the code and align your braces {}.
If you keep the Right Brace } aligned below the Left Brace { you can see better how the blocks of code belong together.
Control key combination control+K, control+D is Klean Document and will fix up a bunch of your indentation and alignement.

This post has been edited by tlhIn`toq: 28 October 2014 - 03:52 AM

Was This Post Helpful? 2
  • +
  • -

#14 pavsingh   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 27-October 14

Re: How to nest try/catch blocks

Posted 28 October 2014 - 06:29 AM

i not getting success. if you guys see my code and fix that instead of suggestions please.
Was This Post Helpful? -1
  • +
  • -

#15 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14409
  • View blog
  • Posts: 57,772
  • Joined: 12-June 08

Re: How to nest try/catch blocks

Posted 28 October 2014 - 06:36 AM

Please be cognizant of the rules regarding asking people to do your work for you.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2