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.
private void menuItem2_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 error if (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 mileage if (bMile >= eMile) { this.label6.Text = "End mileage less than begin mileage "; } //if negative values are entered if (gas < 0) { this.label6.Text = "Gallons is 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 (FormatException ex) { // string was not a number, handle the error or just use a formatted text box, // NumericUpDown, etc. }
}
|
Amit ChoudharyPosted Apr 28, 2010, 12:01 PM
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.
Kenshin HatakePosted Apr 28, 2010, 1:30 PM
Jaish MathewsPosted Apr 28, 2010, 12:01 PM
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 ");
}