新手:关于异常的问题,一题老报错,不明白
#include <iostream>
using namespace std;
//ClassDivideByZeroException to be used in eception
class DivideByZeroException
{
public:
DivideByZeroException()
:message("attempted to divide by zero"){}
const char *what() const{return message;}
private:
const char *message;
};
//Definition of function quotient.Demonstrates throwing
double quotient(int numerator, int denominator)
{
if (denominator = 0)
throw DivideByZeroException();
return static_cast <double> (numerator) / denominator;
}
//driver program
int main()
{
int number1, number2;
double result;
cout << "Enter tow integers(end-of-file to end):";
while (cin >> number1 >> number2)
{
//the try block wraps the code that way throw an
//exception and the code that should not execute
//if an exception occurs
try
{
result = quotient(number1, number2);
cout << "The quotient is: " << result << endl;
}
catch(DivideByZeroException ex)
{//exception handler
cout << "Exception occurred: " << ex.what() << '\n';
}
cout << "\nEnter two integers (end-of-file to end): ";
}
cout << endl;
//system("pause");
return 0; //terminate normally
}
这是小弟看书照书写的,但是运算结果不正常1.#INF,大家帮看看