diff options
Diffstat (limited to 'ACE/contrib/utility/Example/ExH')
-rw-r--r-- | ACE/contrib/utility/Example/ExH/BadCast/Makefile | 24 | ||||
-rw-r--r-- | ACE/contrib/utility/Example/ExH/BadCast/bad_cast.cpp | 52 | ||||
-rw-r--r-- | ACE/contrib/utility/Example/ExH/Compound/Makefile | 24 | ||||
-rw-r--r-- | ACE/contrib/utility/Example/ExH/Compound/compound.cpp | 142 | ||||
-rw-r--r-- | ACE/contrib/utility/Example/ExH/HelloWorld/Makefile | 24 | ||||
-rw-r--r-- | ACE/contrib/utility/Example/ExH/HelloWorld/hello_world.cpp | 141 | ||||
-rw-r--r-- | ACE/contrib/utility/Example/ExH/LogicToSystem/Makefile | 24 | ||||
-rw-r--r-- | ACE/contrib/utility/Example/ExH/LogicToSystem/logic_to_system.cpp | 58 | ||||
-rw-r--r-- | ACE/contrib/utility/Example/ExH/Makefile | 16 |
9 files changed, 505 insertions, 0 deletions
diff --git a/ACE/contrib/utility/Example/ExH/BadCast/Makefile b/ACE/contrib/utility/Example/ExH/BadCast/Makefile new file mode 100644 index 00000000000..9963d9708a1 --- /dev/null +++ b/ACE/contrib/utility/Example/ExH/BadCast/Makefile @@ -0,0 +1,24 @@ +# file : Example/ExH/BadCast/Makefile +# author : Boris Kolpackov <boris@kolpackov.net> +# copyright : Copyright (c) 2002-2003 Boris Kolpackov +# license : http://kolpackov.net/license.html + +root := ../../.. + +include $(root)/BuildRules/Bootstrap.rules + +$(call include, $(root)/BuildRules/Executable.pre.rules) + + +cxx_translation_units := bad_cast.cpp + +module_base := bad_cast +module_prefix := +module_suffix := + + +CXX_PREPROCESS_FLAGS += -I $(root) + + +$(call include, $(root)/BuildRules/Executable.post.rules) +# $Id$ diff --git a/ACE/contrib/utility/Example/ExH/BadCast/bad_cast.cpp b/ACE/contrib/utility/Example/ExH/BadCast/bad_cast.cpp new file mode 100644 index 00000000000..cb4a59e26a9 --- /dev/null +++ b/ACE/contrib/utility/Example/ExH/BadCast/bad_cast.cpp @@ -0,0 +1,52 @@ +// file : Example/ExH/BadCast/bad_cast.cpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#include "Utility/ExH/System/Exception.hpp" + +#include <iostream> + +using std::cerr; +using std::endl; + + +struct A +{ + virtual + ~A() {} +}; + +struct B +{ + void + foo () {} +}; + +void +foo () throw (Utility::ExH::System::Exception) +{ + A a; + + A& ar (a); + + B& br (dynamic_cast<B&> (ar)); + + br.foo (); +} + +int +main () +{ + try + { + foo (); + } + catch (Utility::ExH::System::Exception const& ex) + { + cerr << "Caught Utility::ExH::System::Exception: " + << ex.what () + << endl; + } +} +//$Id$ diff --git a/ACE/contrib/utility/Example/ExH/Compound/Makefile b/ACE/contrib/utility/Example/ExH/Compound/Makefile new file mode 100644 index 00000000000..8bd588587d5 --- /dev/null +++ b/ACE/contrib/utility/Example/ExH/Compound/Makefile @@ -0,0 +1,24 @@ +# file : Example/ExH/Compound/Makefile +# author : Boris Kolpackov <boris@kolpackov.net> +# copyright : Copyright (c) 2002-2003 Boris Kolpackov +# license : http://kolpackov.net/license.html + +root := ../../.. + +include $(root)/BuildRules/Bootstrap.rules + +$(call include, $(root)/BuildRules/Executable.pre.rules) + + +cxx_translation_units := compound.cpp + +module_base := compound +module_prefix := +module_suffix := + + +CXX_PREPROCESS_FLAGS += -I $(root) + + +$(call include, $(root)/BuildRules/Executable.post.rules) +# $Id$ diff --git a/ACE/contrib/utility/Example/ExH/Compound/compound.cpp b/ACE/contrib/utility/Example/ExH/Compound/compound.cpp new file mode 100644 index 00000000000..9e042ca9c1e --- /dev/null +++ b/ACE/contrib/utility/Example/ExH/Compound/compound.cpp @@ -0,0 +1,142 @@ +// file : Example/ExH/Compound/compound.cpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#include "Utility/ExH/Compound.hpp" +#include "Utility/ExH/System/Exception.hpp" +#include "Utility/ExH/Logic/DescriptiveException.hpp" + +// Include some helper converters to allow exception initialization +// with std::ostringstream + +#include "Utility/ExH/StringStreamConverter.hpp" + +#include <iostream> + +using std::cerr; +using std::endl; +using std::string; +using std::ostringstream; + +using namespace Utility::ExH; + +// Here are our components + +class Base +{ +public: + + // + // Exception definitions. + // + + // Base logic exception class for component. + class Exception_ {}; + typedef + Compound <Exception_, Logic::DescriptiveException> + Exception; + + class InvalidArgument_ {}; + typedef + Compound <InvalidArgument_, Exception> + InvalidArgument; + + class NotInitialized_ {}; + typedef + Compound <NotInitialized_, Exception> + NotInitialized; + +public: + + void + foo (char const* str) throw (InvalidArgument, NotInitialized) + { + // This is just an example. + + if (str == 0) + { + throw InvalidArgument ("Base::foo: first parameter is zero."); + } + else + { + ostringstream ostr; + ostr << "Base::foo [this = " << this << "]: object is not initialized."; + + throw NotInitialized (ostr); + } + } + + + // We don't know what implementation may decide to throw so + // we allow to throw System exception and any logic exception + // derived from Base::Exception + virtual void + vfoo () throw (Exception, System::Exception) = 0; +}; + +class Derived : public Base +{ +public: + + // Define some Derived-specific logic exception. + class NotImplemented_ {}; + typedef + Compound <NotImplemented_, Exception> + NotImplemented; + +public: + virtual void + vfoo () throw (NotImplemented, System::Exception) + { + std::string str ("Derived::vfoo: not implemented yet."); + throw NotImplemented (str); + } +}; + +int +main () +{ + try + { + + Derived d; + Base* pb (&d); + + // We can use generic handler. + try + { + pb->vfoo (); + } + catch (Base::Exception const& ex) + { + cerr << "Caught Base::Exception: " << ex.what () << endl; + } + + + // Or use more precise control. + try + { + pb->foo ("hello"); + } + catch (Base::NotInitialized const& ex) + { + cerr << "Caught Base::NotInitialized: " << ex.what () << endl; + } + + // Or use application-level handler. + pb->foo (0); + + } + catch (Logic::Exception const& ex) + { + cerr << "Caught Logic::Exception: " << ex.what () << endl; + } + catch (...) + { + cerr << "Caught unknown exception using catch-all handler" << endl; + return -1; + } + +} +//$Id$ diff --git a/ACE/contrib/utility/Example/ExH/HelloWorld/Makefile b/ACE/contrib/utility/Example/ExH/HelloWorld/Makefile new file mode 100644 index 00000000000..93debef0e7c --- /dev/null +++ b/ACE/contrib/utility/Example/ExH/HelloWorld/Makefile @@ -0,0 +1,24 @@ +# file : Example/ExH/HelloWorld/Makefile +# author : Boris Kolpackov <boris@kolpackov.net> +# copyright : Copyright (c) 2002-2003 Boris Kolpackov +# license : http://kolpackov.net/license.html + +root := ../../.. + +include $(root)/BuildRules/Bootstrap.rules + +$(call include, $(root)/BuildRules/Executable.pre.rules) + + +cxx_translation_units := hello_world.cpp + +module_base := hello_world +module_prefix := +module_suffix := + + +CXX_PREPROCESS_FLAGS += -I $(root) + + +$(call include, $(root)/BuildRules/Executable.post.rules) +# $Id$ diff --git a/ACE/contrib/utility/Example/ExH/HelloWorld/hello_world.cpp b/ACE/contrib/utility/Example/ExH/HelloWorld/hello_world.cpp new file mode 100644 index 00000000000..ee678ffa66f --- /dev/null +++ b/ACE/contrib/utility/Example/ExH/HelloWorld/hello_world.cpp @@ -0,0 +1,141 @@ +// file : Example/ExH/HelloWorld/hello_world.cpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#include <cstdlib> // for std::abort () + +#include <string> +#include <iostream> + +#include "Utility/ExH/System/Exception.hpp" +#include "Utility/ExH/Logic/Exception.hpp" + +using std::cerr; +using std::cout; +using std::endl; + +using namespace Utility; + +class Application +{ +public: + class Exception : public ExH::Logic::Exception {}; + + // Hint: you may want to try again... + class FeelingDizzy : public Exception {}; + + class InvalidArg : public Exception {}; + +public: + Application () throw (ExH::System::Exception) + : // The std::string c-tor may throw any kind of exceptions besides + // quite possible std::bad_alloc. + greeting_ ("Hello, world!") + { + } + + Application (char const * greeting) throw (InvalidArg, + ExH::System::Exception) + : greeting_ (greeting == 0 ? "" : greeting) + { + if (greeting == 0) throw InvalidArg (); + } + +public: + + void + run () throw (FeelingDizzy, ExH::System::Exception) + { + static unsigned int dizzy_count (0); + + if (dizzy_count++ < 5) throw FeelingDizzy (); + + // The next line can throw full bucket of exceptions + // not to mention ios_base::failure. + cout << greeting_.c_str () << endl; + } + +private: + + std::string greeting_; +}; + + + +int +main () +{ + // This is a catch-all layer that should be in use only + // if we are really in trouble. + try + { + // This is a catch-system layer. Here we will catch exceptions like + // bad_alloc, etc. If we get here it means that nobody wanted/managed + // to recover from this kind of errors. + try + { + // This is a catch-logic layer. If we get here it usually + // indicates an application logic error. + try + { + + // Ok, here we go about our application logic. + try + { + for (int i = 0; i < 10; i++) + { + try + { + Application app ("Hi dude!"); + app.run (); + break; + } + catch (Application::FeelingDizzy const& ) + { + if (i == 9) + { + cerr << "Given up!" << endl; + return -1; + } + else + { + cerr << "Application is feeling dizzy. Trying again..." + << endl; + } + } + } + } + catch (Application::InvalidArg const& ) + { + cerr << "Cought Application::InvalidArg : ...hmm... strange!" + << endl; + return -1; + } + } + catch (ExH::Logic::Exception const& e) + { + cerr << "Caught Logic::Exception : " << e.what () << endl; + return -1; + } + } + catch (const ExH::System::Exception& e) + { + cerr << "Caught System::Exception : " << e.what () << endl; + return -1; + } + catch (...) + { + cerr << "Caught unknown exception using catch-all handler. " << endl; + return -1; + } + } + catch (...) + { + // We get here in cases of some hard failure. For example when handling + // exception, operator << throws another exception. Usually application + // cannot handle such failures itself so we just propagate it futher. + std::abort (); + } +} +//$Id$ diff --git a/ACE/contrib/utility/Example/ExH/LogicToSystem/Makefile b/ACE/contrib/utility/Example/ExH/LogicToSystem/Makefile new file mode 100644 index 00000000000..789413c0a3c --- /dev/null +++ b/ACE/contrib/utility/Example/ExH/LogicToSystem/Makefile @@ -0,0 +1,24 @@ +# file : Example/ExH/LogicToSystem/Makefile +# author : Boris Kolpackov <boris@kolpackov.net> +# copyright : Copyright (c) 2002-2003 Boris Kolpackov +# license : http://kolpackov.net/license.html + +root := ../../.. + +include $(root)/BuildRules/Bootstrap.rules + +$(call include, $(root)/BuildRules/Executable.pre.rules) + + +cxx_translation_units := logic_to_system.cpp + +module_base := logic_to_system +module_prefix := +module_suffix := + + +CXX_PREPROCESS_FLAGS += -I $(root) + + +$(call include, $(root)/BuildRules/Executable.post.rules) +# $Id$ diff --git a/ACE/contrib/utility/Example/ExH/LogicToSystem/logic_to_system.cpp b/ACE/contrib/utility/Example/ExH/LogicToSystem/logic_to_system.cpp new file mode 100644 index 00000000000..ba5944404f0 --- /dev/null +++ b/ACE/contrib/utility/Example/ExH/LogicToSystem/logic_to_system.cpp @@ -0,0 +1,58 @@ +// file : Example/ExH/LogicToSystem/logic_to_system.cpp +// author : Boris Kolpackov <boris@kolpackov.net> +// copyright : Copyright (c) 2002-2003 Boris Kolpackov +// license : http://kolpackov.net/license.html + +#include "Utility/ExH/System/Exception.hpp" +#include "Utility/ExH/Logic/Exception.hpp" + +#include <iostream> + +using std::cerr; +using std::endl; + + +struct SubsystemA +{ + class Exception : public Utility::ExH::Logic::Exception {}; + + void + foo () throw (Exception) + { + throw Exception (); + } +}; + + +struct SubsystemB +{ + void + foo () throw (Utility::ExH::System::Exception) + { + SubsystemA a; + a.foo (); + + // Here SubsystemB is using SunsystemA but cannot (forgot, doesnt't + // want to, doesn't know how to, etc - pick your favorite) handle + // exception thrown by SubsystemA. As a result exception is + // 'converted' to System::Exception. + } +}; + + +int +main () +{ + try + { + SubsystemB b; + b.foo (); + } + catch (Utility::ExH::System::Exception const& ex) + { + cerr << "Caught Utility::ExH::System::Exception: " + << ex.what () + << endl; + } +} +//$Id$ diff --git a/ACE/contrib/utility/Example/ExH/Makefile b/ACE/contrib/utility/Example/ExH/Makefile new file mode 100644 index 00000000000..99eb95cf62f --- /dev/null +++ b/ACE/contrib/utility/Example/ExH/Makefile @@ -0,0 +1,16 @@ +# file : Example/ExH/Makefile +# author : Boris Kolpackov <boris@kolpackov.net> +# copyright : Copyright (c) 2002-2003 Boris Kolpackov +# license : http://kolpackov.net/license.html + +root := ../.. + +include $(root)/BuildRules/Bootstrap.rules + +$(call include, $(root)/BuildRules/Recursion.pre.rules) + +target_makefile_list := +target_directory_list := BadCast Compound HelloWorld LogicToSystem + +$(call include, $(root)/BuildRules/Recursion.post.rules) +# $Id$ |