PDA

View Full Version : exception عمل نمیکنه



Developer Programmer
دوشنبه 09 دی 1387, 01: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;
}

Developer Programmer
دوشنبه 09 دی 1387, 09:56 صبح
چه جالب !! خطای DeiveByZero فقط در مورد اعداد Integer اتفاق می افته ! یعنی اگه در مورد دو تا عدد اعشاری این خطا رخ بده، کنترل به Exception داده نمی شه !

این یه شوخیه یا یه باگ ؟!

Amir Oveisi
دوشنبه 09 دی 1387, 19:50 عصر
برگرفته از MSDN:


DivideByZeroException Class
The exception that is thrown when there is an attempt to divide an integral or decimal value by zero.
...
Dividing a floating-point value by zero will result in either positive infinity, negative infinity, or Not-a-Number (NaN) according to the rules of IEEE 754 arithmetic. Floating-point operations never throw an exception.

پس باگ نیست (شوخی هم نیست لابد)

موفق باشید
jooje