From a58c06a102aeb1724909c43e1457f95daddc08f2 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 29 Mar 2014 14:51:23 +0100 Subject: [PATCH] Adds comparison with the classical way --- example.cpp | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/example.cpp b/example.cpp index 2e41e2c..5b624c5 100644 --- a/example.cpp +++ b/example.cpp @@ -1,6 +1,9 @@ #include #include +#include +#include +#include #include "exceptions.h" @@ -10,6 +13,21 @@ EXCEPTION( Exception, Existential_Observation ); EXCEPTION( Buddhist_Observation, Zen_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() { try { @@ -17,11 +35,25 @@ int main() // Use this macro to easily throw an exception with a dynamic message 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) ); } catch( Existential_Observation & e ) { 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; + } }