[snip]
| > | class MyError{};
| > | throw MyError();
| > |
| > | The most general form is still catch(...).
| >
| > Are you saying that 'catch( ... );' should be used ?
| >
| > ...rather than: catch( const MyError& e ); ?
|
| Yes, the reasoning behind that is as follows:
|
| - it is not guaranteed that a compiler generates code such that an exception
| is thrown if an arithmetic exception (such as "divide by zero") is
| encountered during run-time.
That part I know, I wasn't questioning this aspect of it
.
| > AFAIU, the former should only be used when you don't
| > know what will be thrown.
|
| - if the compiler generates such code, it is not known of what type the
| exception is, in particular we don't know whether or not this exception is
| derived from std::exception.
I was asking, why you would use only 'catch( ... );', rather
than 'catch( const MyError& e );', because, if you use a
conditional check, then you get to choose the exception
thrown.
For example:
try
{
int a = 0, b = 9;
if( a == 0 )
throw MyError( "Divide by zero encountered. " );
b /= a;
}
catch( const MyError& e )
{
cout << e.what() << endl;
}
catch( ... )
{
cout << "Something unknown happened" << endl;
}
I am asking, why use only 'catch( ... );' for the above, when
1) it won't catch it,