Adds comparison with the classical way
This commit is contained in:
parent
0343b98552
commit
a58c06a102
1 changed files with 33 additions and 1 deletions
34
example.cpp
34
example.cpp
|
|
@ -1,6 +1,9 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "exceptions.h"
|
#include "exceptions.h"
|
||||||
|
|
||||||
|
|
@ -10,6 +13,21 @@ EXCEPTION( Exception, Existential_Observation );
|
||||||
EXCEPTION( Buddhist_Observation, Zen_Observation );
|
EXCEPTION( Buddhist_Observation, Zen_Observation );
|
||||||
EXCEPTION( Existential_Observation, Pastafarism_Observation );
|
EXCEPTION( Existential_Observation, Pastafarism_Observation );
|
||||||
|
|
||||||
|
|
||||||
|
// A somewhat similar code without this tool would be:
|
||||||
|
// Inherit from runtime_error, because it has a virtual `what` interface.
|
||||||
|
class _Existential_Observation : public std::runtime_error
|
||||||
|
{ public:
|
||||||
|
// Pass through constructors
|
||||||
|
_Existential_Observation( const std::string & what_arg ) : std::runtime_error(what_arg) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class _Buddhist_Observation : public _Existential_Observation
|
||||||
|
{ public:
|
||||||
|
_Buddhist_Observation( const std::string & what_arg ) : _Existential_Observation(what_arg) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|
@ -17,11 +35,25 @@ int main()
|
||||||
// Use this macro to easily throw an exception with a dynamic message
|
// Use this macro to easily throw an exception with a dynamic message
|
||||||
RAISE( Pastafarism_Observation, std::setprecision(3) << "Shit happens with " << pi << " noodly appendages" );
|
RAISE( Pastafarism_Observation, std::setprecision(3) << "Shit happens with " << pi << " noodly appendages" );
|
||||||
|
|
||||||
// Or, if you prefer, use the standard way, but you'll have to indicate E_INFOS yourself
|
// Or, if you prefer, use the standard `throw` function, but you'll have to indicate E_INFOS by yourself
|
||||||
throw( Zen_Observation( "What is the sound of shit happening?", E_INFOS) );
|
throw( Zen_Observation( "What is the sound of shit happening?", E_INFOS) );
|
||||||
|
|
||||||
} catch( Existential_Observation & e ) {
|
} catch( Existential_Observation & e ) {
|
||||||
std::cerr << e.what() << std::endl;
|
std::cerr << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Using the standard way, you will have to do
|
||||||
|
try {
|
||||||
|
std::ostringstream msg;
|
||||||
|
double vacuity = 0;
|
||||||
|
// Beware of the hard-coded class name...
|
||||||
|
msg << "There is " << vacuity << " shit happening (<_Buddhist_Observation in " << __FUNCTION__ << " at " << __FILE__ << ":" << __LINE__ << ")";
|
||||||
|
// And don't forget the string conversion.
|
||||||
|
throw( _Buddhist_Observation( msg.str() ) );
|
||||||
|
|
||||||
|
} catch( _Existential_Observation & e ) {
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue