Developer Programmer
دوشنبه 09 دی 1387, 02:54 صبح
میخوام زمانی که خطای "تقسیم بر صفر" اتفاق افتاد با خبر بشم. منتها، exception عمل نمیکنه
private char ExOperator = ' ';
private Single Operand = 0;
private string Result
{
get
{
return edtResult.Text;
}
set
{
edtResult.Text=value;
}
}
...
private void OperatorPressed(Char op)
{
try
{
switch (ExOperator)
{
case '+':
Result = Convert.ToString( Operand + Convert.ToSingle( Result) );
break;
case '-' :
Result = Convert.ToString( Operand - Convert.ToSingle( Result) );
break;
case '/':
Result = Convert.ToString( Operand / Convert.ToSingle( Result) );
break;
case '*':
Result = Convert.ToString(Operand * Convert.ToSingle( Result) );
break;
case '=' :
// Do nothing !!!
break;
}
}
catch( DivideByZeroException dx)
{
MessageBox.Show( "Divided by zero" + "\n\r" + " result lost" ," ");
Result = "0";
ExOperator = ' ';
}
catch (OverflowException ox)
{
MessageBox.Show("Overflow!" + "\n\r" + " result lost", " ");
Result = "0";
ExOperator = ' ';
}
catch (ArithmeticException ax)
{
MessageBox.Show("ArithmeticException!" + "\n\r" + " result lost", " ");
Result = "0";
ExOperator = ' ';
}
Operand = Convert.ToSingle(Result);
ExOperator = op;
}
private char ExOperator = ' ';
private Single Operand = 0;
private string Result
{
get
{
return edtResult.Text;
}
set
{
edtResult.Text=value;
}
}
...
private void OperatorPressed(Char op)
{
try
{
switch (ExOperator)
{
case '+':
Result = Convert.ToString( Operand + Convert.ToSingle( Result) );
break;
case '-' :
Result = Convert.ToString( Operand - Convert.ToSingle( Result) );
break;
case '/':
Result = Convert.ToString( Operand / Convert.ToSingle( Result) );
break;
case '*':
Result = Convert.ToString(Operand * Convert.ToSingle( Result) );
break;
case '=' :
// Do nothing !!!
break;
}
}
catch( DivideByZeroException dx)
{
MessageBox.Show( "Divided by zero" + "\n\r" + " result lost" ," ");
Result = "0";
ExOperator = ' ';
}
catch (OverflowException ox)
{
MessageBox.Show("Overflow!" + "\n\r" + " result lost", " ");
Result = "0";
ExOperator = ' ';
}
catch (ArithmeticException ax)
{
MessageBox.Show("ArithmeticException!" + "\n\r" + " result lost", " ");
Result = "0";
ExOperator = ' ';
}
Operand = Convert.ToSingle(Result);
ExOperator = op;
}