5 Replies - 442 Views - Last Post: 02 February 2013 - 08:52 PM Rate Topic: -----

#1 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Try Catch While Retry!

Posted 02 February 2013 - 02:59 PM

Hi. My main window open another custom window, and I have to enter 5 different text fields. If the number enter is negative then it would catch an error, and must return to that custom window to re-enter the correct one.

Here is the place where it catch an error, but I don't know how to do it correctly.
        public StockPrice getStockPrice()
        {
            StockPrice s = new StockPrice();
            while (true){
            try
            {
                s.CompanyName = companyNameTB.Text;
                s.Stock = stockTB.Text;
                s.OpeningPrice = double.Parse(openingPriceTB.Text);
                s.HighPrice = double.Parse(highPriceTB.Text);
                s.LowPrice = double.Parse(lowPriceTB.Text);
                s.ClosingPrice = double.Parse(closePriceTB.Text);
                break;
            }
            catch (Exception ex)
            {
               MessageBox.Show(this, "Error: " + ex.Message);
            }}
            return s;
        }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Try Catch While Retry!

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9094
  • View blog
  • Posts: 34,147
  • Joined: 12-June 08

Re: Try Catch While Retry!

Posted 02 February 2013 - 03:06 PM

Why not validate the input before you open the form? You know what is acceptable and what isn't ahead of time so a small function that checks is the best solution.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Try Catch While Retry!

Posted 02 February 2013 - 03:37 PM

Using textboxes for numeric input is a common mistake.
You probably want to replace those textboxes with NumericUpdowns and set the minimum to 0 so it is impossible for the user to enter a negative number. This completely eliminates a mountain of validation code.

I also have a lot of doubts about the planning of this application.
Why would you design it in a way that you have have textboxes on your main form for all these values - but you have to open a different form to make changes in those textboxes? Just use the ones you are already looking at. Poor design like this is what makes users hate some programs.

Next comes the use of a double for money values. Bad. double is prone to rounding errors. You should use decimal instead.
Was This Post Helpful? 0
  • +
  • -

#4 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: Try Catch While Retry!

Posted 02 February 2013 - 06:43 PM

View PosttlhIn`toq, on 02 February 2013 - 03:37 PM, said:

Using textboxes for numeric input is a common mistake.
You probably want to replace those textboxes with NumericUpdowns and set the minimum to 0 so it is impossible for the user to enter a negative number. This completely eliminates a mountain of validation code.

I also have a lot of doubts about the planning of this application.
Why would you design it in a way that you have have textboxes on your main form for all these values - but you have to open a different form to make changes in those textboxes? Just use the ones you are already looking at. Poor design like this is what makes users hate some programs.

Next comes the use of a double for money values. Bad. double is prone to rounding errors. You should use decimal instead.


Because that how my teacher wanted. He want another window form

View Postmodi123_1, on 02 February 2013 - 03:06 PM, said:

Why not validate the input before you open the form? You know what is acceptable and what isn't ahead of time so a small function that checks is the best solution.


Yeah I will try to work with this validate
Was This Post Helpful? 0
  • +
  • -

#5 DaiToBu  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 17-March 12

Re: Try Catch While Retry!

Posted 02 February 2013 - 07:57 PM

Thank for the help. I found the problem. The reason I think of is I already did the try and catch in the main form, and I did try and catch again on the custom window. I think that the reason why it mess up. So I remove the try and catch above (custom window), and it work for me.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,009
  • Joined: 05-May 12

Re: Try Catch While Retry!

Posted 02 February 2013 - 08:52 PM

Using exceptions for flow control is really poor coding craftsmanship. Exceptions should be for truly exceptional conditions. If you are expecting the possibility of failing to parse numbers, use TryParse() instead of Parse().
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1