Exception.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Utils_Exception_
18 #define HH_SENF_Utils_Exception_ 1
19 
20 // Custom includes
21 #include <exception>
22 #include <stdexcept>
23 #include <string>
24 #include <boost/utility/enable_if.hpp>
25 #include <boost/type_traits/is_convertible.hpp>
26 #include <senf/config.hh>
27 
28 //#include "Exception.mpp"
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
120 namespace senf {
121 
130  {
131  public:
132  std::string message() const;
133  std::string backtrace() const;
134 
135  void append(std::string text);
136 
138  protected:
139  explicit ExceptionMixin(std::string const & description = "");
141 
145  std::string what_;
146 
147  private:
148 #ifdef SENF_BACKTRACE
149  void addBacktrace();
150  std::string::size_type excLen_;
151 #endif
152  };
153 
164  class Exception
165  : public ExceptionMixin, public std::exception
166  {
167  public:
168  virtual ~Exception() throw();
169 
170  virtual char const * what() const throw();
172 
175  protected:
176  explicit Exception(std::string const & description = "");
177  };
178 
186  template <class BaseException>
188  : public ExceptionMixin, public BaseException
189  {
190  public:
191  typedef BaseException Base;
192 
193  WrapException(BaseException const & base);
194  virtual ~WrapException() throw();
195 
196  virtual char const * what() const throw();
197  };
198 
199  template <class BaseException>
200  WrapException<BaseException> make_WrapException(BaseException const & base);
201 
202 
204  : public WrapException<std::invalid_argument>
205  {
206  InvalidArgumentException(std::string const & what_arg);
207  };
208 
209 
217 # define SENF_WRAP_EXC(Ex) \
218  catch (Ex const & base) { \
219  if (dynamic_cast<senf::ExceptionMixin const *>(&base)) \
220  throw; \
221  else \
222  throw senf::WrapException<Ex>(base); \
223  }
224 
225 # define SENF_WRAP_EXC_MSG(Ex, Msg) \
226  catch (Ex const & base) { \
227  if (dynamic_cast<senf::ExceptionMixin const *>(&base)) \
228  throw; \
229  else \
230  throw senf::WrapException<Ex>(base) << " " << Msg; \
231  }
232 
233  template <class Exc, class Arg>
234  typename boost::enable_if< boost::is_convertible<Exc*,ExceptionMixin*>, Exc & >::type
235  operator<<(Exc const & exc, Arg const & arg);
236 
244 # ifdef SENF_DEBUG
245 # define _SENF_EXC_DEBUG_ARGS ,char const * file=0,int line=0
246 # define _SENF_EXC_DEBUG_ARGS_ND ,char const *file,int line
247 # define _SENF_EXC_DEBUG_ARGS_P ,file,line
248 # else
249 # define _SENF_EXC_DEBUG_ARGS
250 # define _SENF_EXC_DEBUG_ARGS_ND
251 # define _SENF_EXC_DEBUG_ARGS_P
252 # endif
253 
286  class SystemException : public Exception
287  {
288  public:
289  //-////////////////////////////////////////////////////////////////////////
291  //\{
292 
293  explicit SystemException(std::string const & descr = "" _SENF_EXC_DEBUG_ARGS);
294  explicit SystemException(int code _SENF_EXC_DEBUG_ARGS);
295  SystemException(std::string const & descr, int code _SENF_EXC_DEBUG_ARGS);
296 
297  virtual ~SystemException() throw();
298 
299  //\}
300  //-////////////////////////////////////////////////////////////////////////
301 
302  int errorNumber() const;
303  char const * errorString() const;
304 
305  bool anyOf(int c0, int c1=0, int c2=0, int c3=0, int c4=0, int c5=0,
306  int c6=0, int c7=0, int c8=0, int c9=0) const;
308 
309  private:
310  void init(std::string const & descr, int code _SENF_EXC_DEBUG_ARGS_ND);
311 
312  int code_;
313  };
314 
315 # ifdef SENF_DEBUG
316 # define SENF_EXC_DEBUGINFO ,__FILE__,__LINE__
317 # else
318 # define SENF_EXC_DEBUGINFO
319 # endif
320 
321 # define SENF_THROW_SYSTEM_EXCEPTION(desc) \
322  throw senf::SystemException(desc SENF_EXC_DEBUGINFO)
323 
324 }
325 
326 //-/////////////////////////////////////////////////////////////////////////////////////////////////
327 #include "Exception.cci"
328 //#include "Exception.ct"
329 #include "Exception.cti"
330 #endif
331 
332 
333 // Local Variables:
334 // mode: c++
335 // fill-column: 100
336 // c-file-style: "senf"
337 // indent-tabs-mode: nil
338 // ispell-local-dictionary: "american"
339 // compile-command: "scons -u test"
340 // comment-column: 40
341 // End:
342 
void init()
STL namespace.
std::string backtrace() const
Return backtrace (if available)
std::ostream & operator<<(std::ostream &os, CpuStat const &cs)
Definition: CpuStat.cc:103
void append(std::string text)
Extend exception description.
Generic extensible exception mixin.
Definition: Exception.hh:129
WrapException< BaseException > make_WrapException(BaseException const &base)
BaseException Base
Definition: Exception.hh:191
unspecified_keyword_type description
ExceptionMixin(std::string const &description="")
Initialize exception with string.
#define _SENF_EXC_DEBUG_ARGS
Definition: Exception.hh:249
#define _SENF_EXC_DEBUG_ARGS_ND
Definition: Exception.hh:250
Wrapper for standard non-senf exceptions.
Definition: Exception.hh:187
Exception handling standard UNIX errors (errno)
Definition: Exception.hh:286
std::string message() const
get exception description
Extensible exception base-class.
Definition: Exception.hh:164
std::string what_
Definition: Exception.hh:145