Ok so I got the error events that I want set up but when I am runing it only the final result works when there are no error in the input data. But for instance when I put 0 in the gas used the results are printed is Infinity, and when I put in any number for the beging or ending miles it does the math regardless of what error are in the input data.
The Error I want to catch are;
gallons in gas and ending miles cannot be = to 0;
beging miles must be smaller than ending miles;
there can be no negative numbers;
and I want to print out what and where the error was entered. I got this set up pretty nice, but forsome reason it does not work the way I have it. I would appreciate any help on this.
C#
privatevoidmenuItem2_Click(object sender, EventArgs e)
{
try
{
float gas = float.Parse(textBox1.Text);
float bMile = float.Parse(textBox2.Text);
float eMile = float.Parse(textBox3.Text);
//if gas or endmiles is 0 errorif (gas == 0 )
{
this.label6.Text = "Gallons is zero ";
}
if(eMile == 0)
{
this.label6.Text = "Ending Mileage is zero ";
}
//see if begining mileage is larger than ending mileageif (bMile >= eMile)
{
this.label6.Text = "End mileage less than begin mileage ";
}
//if negative values are enteredif (gas this.label6.Text = "Gallons is negative ";
}
if (bMile this.label6.Text = "Begning Miles is negative ";
}
if (eMile this.label6.Text = "Ending Miles is negative ";
}
this.label6.Text = String.Format("Result: {0}", (eMile - bMile) / gas);
}
catch (FormatException ex)
{
// string was not a number, handle the error or just use a formatted text box, // NumericUpDown, etc.
}
}
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Amit ChoudharyRank #145 · 15.8K reputationPosted Apr 28, 2010, 8:01 AM EDT
✓ Accepted answer0
hi friend,
Write code like this.. whenever you gas or emile value is not appropriate throw a new exception manually. in catch show a message that you have passed in then new thrown exception.
try {
if(eMile == 0) { throw new ArithMaticException("Miles can not be null"); } //see if begining mileage is larger than ending mileage if (bMile >= eMile) { this.label6.Text = "End mileage less than begin mileage "; } //if negative values are entered if (gas < 0) { throw new ArithmaticException("Gas can't be zero or negative"); } if (bMile < 0) { this.label6.Text = "Begning Miles is negative "; } if (eMile < 0) { this.label6.Text = "Ending Miles is negative "; } this.label6.Text = String.Format("Result: {0}", (eMile - bMile) / gas); } catch (ArithmaticException ex) { // string was not a number, handle the error or just use a formatted text box, this.label6.Text =ex.Message; }
Please mark "Do you like this post" if it helps you.
Hi,
Use an exception class like below public class MyException : ApplicationException
{
public MyException() : base()
{
}
public MyException(string message)
: base(message)
{
}
public MyException(string message, Exception ex)
: base(message,ex)
{
}
}
Call above class when ever you need exception handling. Notice the bolded part where I called. Same aplicable to other scenarios too. try
{
float gas = float.Parse(textBox1.Text);
float bMile = float.Parse(textBox2.Text);
float eMile = float.Parse(textBox3.Text);
//if gas or endmiles is 0 error
if (gas == 0)
{
this.label6.Text = "Gallons is zero ";
throw new MyException("Gallons is zero "); }
Amit ChoudharyRank #145 · 15.8K reputationPosted Apr 28, 2010, 8:01 AM EDT
Write code like this.. whenever you gas or emile value is not appropriate throw a new exception manually. in catch show a message that you have passed in then new thrown exception.
try
{
if(eMile == 0)
{
throw new ArithMaticException("Miles can not be null");
}
//see if begining mileage is larger than ending mileage
if (bMile >= eMile)
{
this.label6.Text = "End mileage less than begin mileage ";
}
//if negative values are entered
if (gas < 0)
{
throw new ArithmaticException("Gas can't be zero or negative");
}
if (bMile < 0)
{
this.label6.Text = "Begning Miles is negative ";
}
if (eMile < 0)
{
this.label6.Text = "Ending Miles is negative ";
}
this.label6.Text = String.Format("Result: {0}", (eMile - bMile) / gas);
}
catch (ArithmaticException ex)
{
// string was not a number, handle the error or just use a formatted text box,
this.label6.Text =ex.Message;
}
Please mark "Do you like this post" if it helps you.
Jaish MathewsRank #225 · 11K reputationPosted Apr 28, 2010, 8:01 AM EDT
Hi,
Use an exception class like below
public class MyException : ApplicationException
{
public MyException() : base()
{
}
public MyException(string message)
: base(message)
{
}
public MyException(string message, Exception ex)
: base(message,ex)
{
}
}
Call above class when ever you need exception handling. Notice the bolded part where I called. Same aplicable to other scenarios too.
try
{
float gas = float.Parse(textBox1.Text);
float bMile = float.Parse(textBox2.Text);
float eMile = float.Parse(textBox3.Text);
//if gas or endmiles is 0 error
if (gas == 0)
{
this.label6.Text = "Gallons is zero ";
throw new MyException("Gallons is zero ");
}
Kenshin Hatake6 reputationPosted Apr 28, 2010, 9:30 AM EDT