From f2b43796ce0b047a4a2b1ae81d7c0d55b1fee2f7 Mon Sep 17 00:00:00 2001 From: florent <florent@laas.fr> Date: Fri, 2 Jul 2010 20:56:42 +0200 Subject: [PATCH] Make ExceptionAbstract derive from std::exception. * include/dynamic-graph/exception-abstract.h, * include/dynamic-graph/exception-factory.h, * include/dynamic-graph/exception-signal.h, * include/dynamic-graph/exception-traces.h, * include/dynamic-graph/signal-caster.h: This enables uses to catch a broader class of exceptions with messages using what() method. --- include/dynamic-graph/exception-abstract.h | 8 ++++++-- include/dynamic-graph/exception-factory.h | 2 +- include/dynamic-graph/exception-signal.h | 2 +- include/dynamic-graph/exception-traces.h | 2 +- include/dynamic-graph/signal-caster.h | 10 +++++++++- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/dynamic-graph/exception-abstract.h b/include/dynamic-graph/exception-abstract.h index 548fb77b..b7e04ef9 100644 --- a/include/dynamic-graph/exception-abstract.h +++ b/include/dynamic-graph/exception-abstract.h @@ -43,7 +43,7 @@ namespace dynamicgraph { /* \class ExceptionAbstract */ -class DYNAMICGRAPH_EXPORT ExceptionAbstract +class DYNAMICGRAPH_EXPORT ExceptionAbstract : public std::exception { public: @@ -74,7 +74,7 @@ private: public: ExceptionAbstract( const int& code, const std::string & msg = "" ); - virtual ~ExceptionAbstract( void ){} + virtual ~ExceptionAbstract( void ) throw() {} /** Access to the error code. */ int getCode (void); @@ -87,6 +87,10 @@ public: */ const char *getMessage (void); + virtual const char* what() const throw() + { + return getStringMessage().c_str(); + } /** Print the error structure. */ DYNAMICGRAPH_EXPORT friend std::ostream & operator << (std::ostream & os, diff --git a/include/dynamic-graph/exception-factory.h b/include/dynamic-graph/exception-factory.h index 20176761..c3c6fd9a 100644 --- a/include/dynamic-graph/exception-factory.h +++ b/include/dynamic-graph/exception-factory.h @@ -64,7 +64,7 @@ public: const std::string & msg = "" ); ExceptionFactory ( const ExceptionFactory::ErrorCodeEnum& errcode, const std::string & msg,const char* format, ... ); - virtual ~ExceptionFactory( void ){} + virtual ~ExceptionFactory( void ) throw() {} }; diff --git a/include/dynamic-graph/exception-signal.h b/include/dynamic-graph/exception-signal.h index fb3d272c..590034c9 100644 --- a/include/dynamic-graph/exception-signal.h +++ b/include/dynamic-graph/exception-signal.h @@ -63,7 +63,7 @@ public: const std::string & msg = "" ); ExceptionSignal( const ExceptionSignal::ErrorCodeEnum& errcode, const std::string & msg,const char* format, ... ); - virtual ~ExceptionSignal( void ){} + virtual ~ExceptionSignal( void ) throw() {} }; diff --git a/include/dynamic-graph/exception-traces.h b/include/dynamic-graph/exception-traces.h index 1eeecba1..a0db511f 100644 --- a/include/dynamic-graph/exception-traces.h +++ b/include/dynamic-graph/exception-traces.h @@ -58,7 +58,7 @@ public: const std::string & msg = "" ); ExceptionTraces( const ExceptionTraces::ErrorCodeEnum& errcode, const std::string & msg,const char* format, ... ); - virtual ~ExceptionTraces( void ){} + virtual ~ExceptionTraces( void ) throw () {} }; diff --git a/include/dynamic-graph/signal-caster.h b/include/dynamic-graph/signal-caster.h index 41f5a8a7..6d9dee19 100644 --- a/include/dynamic-graph/signal-caster.h +++ b/include/dynamic-graph/signal-caster.h @@ -17,6 +17,7 @@ #include <iostream> #include <dynamic-graph/dynamic-graph-api.h> +#include "dynamic-graph/exception-signal.h" namespace dynamicgraph { @@ -101,7 +102,14 @@ template<typename T> void signal_disp(const T& value, std::ostream& os) { g_caster.disp(value, os); } template<typename T> T signal_cast(std::istringstream& iss) - { return boost::any_cast<T>(g_caster.cast(typeid(T), iss)); } + { + try { + return boost::any_cast<T>(g_caster.cast(typeid(T), iss)); + } catch (...) { + throw ExceptionSignal(ExceptionSignal::GENERIC, + "Exception boost::any_cast"); + } + } template<typename T> void signal_trace(const T& value, std::ostream& os) { g_caster.trace(value, os); } -- GitLab