4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief Exception public header */
31 #ifndef HH_SENF_Utils_Exception_
32 #define HH_SENF_Utils_Exception_ 1
39 #include <boost/utility.hpp>
40 #include <boost/type_traits/is_convertible.hpp>
41 #include <senf/config.hh>
43 //#include "Exception.mpp"
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 /** \defgroup exception Exception classes
48 All exceptions in %senf are derived from senf::Exception. This class adds the possibility to
49 extend the exception description while it is processed. The functionality is provided by a mixin
50 class senf::ExceptionMixin:
55 // Some code which might raise an arbitrary senf exception
58 catch (senf::ExceptionMixin & e) {
59 e << "\handling user " << user;
64 This will add the user information to any %senf exception thrown. The Exception is however not a
65 stream. If you need to do more extensive formating, either use an intermediate string-stream or
66 use <a href="http://www.boost.org/doc/libs/release/libs/format/index.html">Boost.Format</a>:
72 catch (senf::ExceptionMixin & e) {
73 e << boost::format("\n" "call id 0x%04x@%s") % id % address;
77 senf::SystemException is thrown for all operating system errors (failures which result in the
78 operating system setting the errno value). It is also derived from senf::Exception and can
79 therefore be extended as well.
81 Defining your own exception classes derived from senf::Exception is very simple:
84 struct FooException : public senf::Exception
85 { FooException() : senf::Exception("Foo hit the fan") {} };
88 If SENF is compiled in debug mode (SENF_DEBUG is defined), the exception message will
89 automatically include a stack backtrace. For this to work, you need to add the
90 <tt>-rdynamic</tt> option to all link commands. This feature depends on <tt>gcc</tt> and
93 To apply these features (extensibility, backtrace) to a non-senf exception, the non-senf
94 exception can be wrapped and re-thrown.
98 // ... code that might throw std::bad_cast or somelib::FooException
100 SENF_WRAP_EXC(std::bad_cast)
101 SENF_WRAP_EXC(somelib::FooException)
104 The re-thrown exception can then be caught as <tt>std::bad_cast</tt> or as senf::ExceptionMixin
105 as needed. It is safe, to wrap an exception twice (the macro will detect this case).
112 catch (senf::ExceptionMixin & ex) {
113 ex << "\n" "add this info";
116 catch (std::bad_cast const & ex) {
117 std::cerr << ex.what() << std::endl;
120 The final error output will include
121 \li a backtrace if compiled in debug mode
122 \li the original error message from the <tt>std::bad_cast</tt> exception
123 \li the additional error message "add this info"
125 \todo Link against libcwd to add file-name/line-number information to the backtrace and remove
126 the dependency on -rdynamic
127 \todo Or better, use addr2line to obtain that information when showing the backtrace when
128 catched within Daemon (<tt>addr2line -fsiCe argv[0]</tt>)
129 \todo Add signal handlers for the bad signals which writes a backtrace to stderr and
130 terminates. This should probably write out a raw backtrace without de-mangling or
131 line-numbers since we don't want to mess with dynamic memory when the heap might be
132 corrupted ... Another handler for e.g. SIGUSR2 is nice to show a debug backtrace on demand
137 /** \brief Generic extensible exception mixin
139 ExceptionMixin is a generic exception mixin which allows the exception to be later extended
140 by catching and re-throwing it (See example in \ref exception).
147 std::string message() const; ///< get exception description
148 std::string backtrace() const; ///< Return backtrace (if available)
150 void append(std::string text); ///< Extend exception description
151 /**< Adds \a text to the description text. */
154 explicit ExceptionMixin(std::string const & description = "");
155 ///< Initialize exception with string
156 /**< \a description is the initial error description
157 string. This should probably be a string constant
158 describing the exception for most derived
163 #ifdef SENF_BACKTRACE
167 std::string::size_type excLen_;
171 /** \brief Extensible exception base-class
173 This base-class is an exception which already includes the ExceptionMixin. All SENF
174 exceptions are derived from this class. Other user-exception may be defined by deriving from
182 : public ExceptionMixin, public std::exception
185 virtual ~Exception() throw();
187 virtual char const * what() const throw();
188 ///< get exception description and backtrace if available
189 /**< get description of the exception (message()) and backtrace
190 information if SENF is compiled with \c SENF_DEBUG */
193 explicit Exception(std::string const & description = "");
196 /** \brief Wrapper for standard non-senf exceptions
198 This class wraps an exception of type \a BaseException and adds functionality from
199 senf::ExceptionMixin.
203 template <class BaseException>
205 : public ExceptionMixin, public BaseException
208 typedef BaseException Base;
210 WrapException(BaseException const & base);
211 virtual ~WrapException() throw();
213 virtual char const * what() const throw();
216 /** \brief Wrap a non-senf exception
218 This macro allows to wrap a non-senf exception adding functionality from ExceptionMixin
219 using the WrapException template. For an example, see \ref exception.
223 # define SENF_WRAP_EXC(Ex) \
224 catch (Ex const & base) { \
225 if (dynamic_cast<senf::ExceptionMixin const *>(&base)) \
228 throw senf::WrapException<Ex>(base); \
231 template <class Exc, class Arg>
232 typename boost::enable_if< boost::is_convertible<Exc*,ExceptionMixin*>, Exc & >::type
233 operator<<(Exc const & exc, Arg const & arg); ///< Extend exception description
234 /**< Adds \a arg converted to string to the end of the
235 exception description string. This operator allows to
236 use Exception instances like streams. The conversion is
237 performed using <code>boost::lexical_cast</code> and is
238 therefor identical to a streaming operation.
239 \see \ref exception */
243 # define _SENF_EXC_DEBUG_ARGS ,char const * file=0,int line=0
244 # define _SENF_EXC_DEBUG_ARGS_ND ,char const *file,int line
245 # define _SENF_EXC_DEBUG_ARGS_P ,file,line
247 # define _SENF_EXC_DEBUG_ARGS
248 # define _SENF_EXC_DEBUG_ARGS_ND
249 # define _SENF_EXC_DEBUG_ARGS_P
252 /** \brief Exception handling standard UNIX errors (errno)
254 This exception is thrown to signal generic \c errno failures. Normally the \c errno value is
255 automatically taken from the \c errno variable but it may also be specified explicitly:
258 // Standard usage: Take \c errno from environment
259 throw senf::SystemException("::open()")
260 << " while opening configuration file: " << filename;
262 // You may however explicitly specify the errno value
263 throw senf::SystemException("::open()", ENOFILE)
265 // Or leave the location information empty
266 throw senf::SystemException(ENOFILE);
267 throw senf::SystemException();
270 From within SENF (<em>and only there because it depends on the \c SENF_DEBUG symbol</em>),
271 SystemException should be thrown using wrapper macros which add additional information to
272 the exception description:
274 // Standard usage: Take \c errno from environment
275 SENF_THROW_SYSTEM_EXCEPTION("::open()")
276 << " while opening configuration file: " << filename;
278 // You may however explicitly specify the errno value
279 throw senf::SystemException("::open()", ENOFILE SENF_EXC_DEBUGINFO)
284 class SystemException : public Exception
287 //-////////////////////////////////////////////////////////////////////////
288 ///\name Structors and default members
291 explicit SystemException(std::string const & descr = "" _SENF_EXC_DEBUG_ARGS);
292 explicit SystemException(int code _SENF_EXC_DEBUG_ARGS);
293 SystemException(std::string const & descr, int code _SENF_EXC_DEBUG_ARGS);
295 virtual ~SystemException() throw();
298 //-////////////////////////////////////////////////////////////////////////
300 int errorNumber() const; ///< Error code (\c errno number)
301 char const * errorString() const; ///< Error string (\c strerror() value)
303 bool anyOf(int c0, int c1=0, int c2=0, int c3=0, int c4=0, int c5=0,
304 int c6=0, int c7=0, int c8=0, int c9=0) const;
305 ///< \c true, if errorNumber() is one of \a c0 ... \a c9
308 void init(std::string const & descr, int code _SENF_EXC_DEBUG_ARGS_ND);
314 # define SENF_EXC_DEBUGINFO ,__FILE__,__LINE__
316 # define SENF_EXC_DEBUGINFO
319 # define SENF_THROW_SYSTEM_EXCEPTION(desc) \
320 throw senf::SystemException(desc SENF_EXC_DEBUGINFO)
324 //-/////////////////////////////////////////////////////////////////////////////////////////////////
325 #include "Exception.cci"
326 //#include "Exception.ct"
327 #include "Exception.cti"
334 // c-file-style: "senf"
335 // indent-tabs-mode: nil
336 // ispell-local-dictionary: "american"
337 // compile-command: "scons -u test"
338 // comment-column: 40