-
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";
// }
//}
-
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";
}
-
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)
-
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?
-
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.
-
Thanks, I was able to get things working correctly by nesting my try/catch!