Hi,
Someone can explain to me the diference between CODE_A and CODE_B?
//double calc( int a, int b ) ;
//double calc( int a, int b ) throw(MathError);
why "throw(MathError);"
CODE_A
--------------------------------------------------------------
class MathError
{
private:
string message;
public:
MathError( const string& s) : message(s) {}
const string& getMessage() const {return message;}
};
double calc( int a, int b ) throw(MathError);
int _tmain(int argc, _TCHAR* argv[])
{
int x, y; bool flag = false;
do
{
try // try block
{
cout << "Enter two positive integers: ";
cin >> x >> y;
cout << x <<"/"<< y <<" = "<< calc( x, y) << '\n';
flag = true; // To leave the loop.
}
catch( MathError& err) // catch block
{
cerr << err.getMessage() << endl;
}
catch( ...)
{
cerr << "3333" << endl;
}
}while( !flag);
// continued ...
return 0;
}
double calc( int a, int b ) throw (MathError)
{
if ( b < 0 )
throw MathError("Denominator is negative!");
if( b == 0 )
throw MathError("Division by 0!");
return ((double)a/b);
}
CODE_B
--------------------------------------------------------------
class MathError
{
private:
string message;
public:
MathError( const string& s) : message(s) {}
const string& getMessage() const {return message;}
};
double calc( int a, int b )
int _tmain(int argc, _TCHAR* argv[])
{
int x, y; bool flag = false;
do
{
try // try block
{
cout << "Enter two positive integers: ";
cin >> x >> y;
cout << x <<"/"<< y <<" = "<< calc( x, y) << '\n';
flag = true; // To leave the loop.
}
catch( MathError& err) // catch block
{
cerr << err.getMessage() << endl;
}
catch( ...)
{
cerr << "3333" << endl;
}
}while( !flag);
// continued ...
return 0;
}
double calc( int a, int b )
{
if ( b < 0 )
throw MathError("Denominator is negative!");
if( b == 0 )
throw MathError("Division by 0!");
return ((double)a/b);
}
Thanks