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

Re: ::exception by Lau

Lau
Wed Jun 01 01:08:32 CDT 2005

Quoted From:
ms-help://MS.MSDNQTR.2003FEB.1033/vclang/html/vclrfexceptionspecifications.htm

Exception specifications are used to provide summary information about what
exceptions can be thrown out of a function. For example:
void MyFunction(int i) throw(...);

"re.tf" <re.tf@newsgroup.nospam> ¼¶¼g©ó¶l¥ó·s»D:u4VfePkZFHA.2128@TK2MSFTNGP15.phx.gbl...
> 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
>
>



RE: ::exception by v-garych

v-garych
Wed Jun 01 02:04:13 CDT 2005

Hi,

>//double calc( int a, int b ) ;
>//double calc( int a, int b ) throw(MathError);
>
>why "throw(MathError);"

Just like Lao Lei figured:the "throw(type-list)"(that follows a function
declaration) is an exception specification that is used to tell the
compiler which exceptions the function can throw.

"An exception specification is part of a function's contract with its
caller. The specification enumerates completely which exceptions that
function may throw. (In the parlance of the standard, the function is said
to "allow" the specified exceptions.)

By implication, the function does not allow (promises not to throw) any
exceptions that aren't in the specification. If the specification is
present but empty, the function allows no exceptions at all; conversely, if
the specification is omitted, the function allows all exceptions." Digested
from the Handling Exceptions, Part 6 of the <<Deep C++>> by Robert Schmidt.

Please refer to the following article links for further information:
Handling Exceptions, Part 6
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndeepc/htm
l/deep080599.asp

Exception Specifications
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html
/vclrfexceptionspecifications.asp


Thanks!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.