Thread: try/catch problem

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    42

    try/catch problem

    Im using my text to numerals to text class on a form to set a timer. It needs to change my textbox to pink if it is out of range (100-10000ms) or if it is an invalid entry. It works ok with numerals but fails to work on the invalid characters or out of range characters. My first catch picks up a FormatException on entering any text and my second catch picks up ArgumentOutofRange Exception. I am a bit lost, can anyone give me some help? TIA
    Code:
     private void tRate_TextChanged(object sender, EventArgs e)
            {
    
                
                try
                {
                    tRate.BackColor = Color.White;
                    Demo.Number = int.Parse(tRate.Text);
                    tPeriodic.Interval = Demo.Number;
                    tStatusMessage.Text = "Timer running at " + tPeriodic.Interval + " milliseconds";
                }
                catch
                {
                    if (tPeriodic.Interval < 100 || tPeriodic.Interval > 10000)
                    {
                        tRate.BackColor = Color.Pink;
                        tPeriodic.Interval = 100;
                        tStatusMessage.Text = "Interval out of range, using 100ms";
                    }
                   
    
                }
                try
                {
                    Demo.Text = tRate.Text;
                    tPeriodic.Interval = Demo.Number;
                    tStatusMessage.Text = "Timer running at " + tPeriodic.Interval + " milliseconds";
                }
                catch
                {
                    if (tPeriodic.Interval < 100 || tPeriodic.Interval > 10000)
                    {
                        tRate.BackColor = Color.Pink;
                        tPeriodic.Interval = 100;
                        tStatusMessage.Text = "Interval out of range, using 100ms";
                    }
                }
    
                //finally
                //{
                //    if (tPeriodic.Interval < 100 || tPeriodic.Interval > 10000)
                //    {
                //        tRate.BackColor = Color.Pink;
                //        tPeriodic.Interval = 100;
                //        tStatusMessage.Text = "Interval out of range, using 100ms";
                //    }
                //}

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    IMO, everything you currently have inside your catch-if blocks should be inside the class definition.

    try-catch-finally is for resolving the exception, not determining that it has occurred. Consider the following code.
    Code:
    // inside tPeriodic
    private int interval;
    
    public int Interval
    {
      get { return interval; }
      set
      {
        if (100 > value || value > 10000)
        {
          throw new ArgumentOutOfRangeException();
        } 
        else
          interval = value;
      }
    }
    
    // inside tRate_TextChanged
    catch (ArgumentOutOfRangeException ex)
    {
      tRate.BackColor = Color.Pink;
      tPeriodic.Interval = 100;
      tStatusMessage.Text = "Interval out of range, defaulting to 100 ms";
    }
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Thanks for your insight msh. I tried it inside my tPeriodic and it gave me errors unless I took out what was already there (wich stopped my timer)
    Code:
    private void tPeriodic_Tick(object sender, EventArgs e)
            {
                n++;
                lTickCount.Text = n.ToString();
             }
    I did use it in my numberstring class (where im converting digits to text to digits) "get/set"
    Code:
    class NumberString
        {
            private int _Number;
            private string _Text;
    
            public int Number //"Number" is a property of class "NumberString"
            {
                get
                {
                    return _Number;
                }
                set
                {
                    if (100 > value || value > 10000)
                    {
                        throw new ArgumentOutOfRangeException();
                    }
                    else
                    _Number = value;
                    _Text = numbervalue(value);
                }
            }
    
            public string Text  //"Text" is a property of class "NumberString"
            {
                get
                {
                    return _Text;
                }
                set
                {
                   _Text = value;
                   _Number = digitvalue(value);
                }
    
            }
    now the timer range works when I enter digits in the text box but has no effect when I enter a string such as "one million".

    I know I must be missing something simple. Textbox is set to white and turn it red if anything goes wrong (ie. invalid charcters are entered or if the timer is out of range)

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Sorry for taking this long to respond. If you're still having issues with this, could you use pastebin to post your current code for tPeriodic (and perhaps some other relevant parts) and link it here?
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    try
                {
                    tRate.BackColor = Color.White;
                    Demo.Number = int.Parse(tRate.Text);
                    tPeriodic.Interval = Demo.Number;
                    tStatusMessage.Text = "Timer running at " + tPeriodic.Interval + " milliseconds";
                }
                catch
                {
                    if (tPeriodic.Interval < 100 || tPeriodic.Interval > 10000)
                    {
                        tRate.BackColor = Color.Pink;
                        tPeriodic.Interval = 100;
                        tStatusMessage.Text = "Interval out of range, using 100ms";
                    }
                }
    This code won't work not for design, but simply because the flow chart would not be correct. You should have the range check before you update the tPeriodic.Interval not in the catch block.

    Try searching for int.TryParse() as well. This will avoid using try-catch blocks.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Thanks, I was able to get things working correctly by nesting my try/catch!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM