Thea
Error.hpp
1 //============================================================================
2 //
3 // This file is part of the Thea toolkit.
4 //
5 // This software is distributed under the BSD license, as detailed in the
6 // accompanying LICENSE.txt file. Portions are derived from other works:
7 // their respective licenses and copyright information are reproduced in
8 // LICENSE.txt and/or in the relevant source files.
9 //
10 // Author: Siddhartha Chaudhuri
11 // First version: 2009
12 //
13 //============================================================================
14 
15 #ifndef __Thea_Error_hpp__
16 #define __Thea_Error_hpp__
17 
18 #include "Platform.hpp"
19 #include "BasicStringAlg.hpp"
20 #include "Log.hpp"
21 #include <stdexcept>
22 #include <string>
23 
24 namespace Thea {
25 
27 typedef std::runtime_error Error;
28 
30 class THEA_API FatalError
31 {
32  public:
34  FatalError(std::string const & message_) : message(message_) {}
35 
38 
40  FatalError & operator=(FatalError const & src) { message = src.message; return *this; }
41 
43  char const * what() const { return message.c_str(); }
44 
45  private:
46  std::string message;
47 
48 }; // class FatalError
49 
60 #define THEA_CATCH(action__, stream__, message__, ...) \
61  catch (FatalError const & e__) \
62  { \
63  THEA_ERROR << format( "A fatal error occurred (%s)", e__.what() ); \
64  throw; \
65  } \
66  catch (std::exception const & e__) \
67  { \
68  THEA_ ## stream__ << format( (message__ + std::string(" (%s)")).c_str(), __VA_ARGS__, e__.what() ); \
69  action__ \
70  } \
71  catch (std::string const & e__) \
72  { \
73  THEA_ ## stream__ << format( (message__ + std::string(" (%s)")).c_str(), __VA_ARGS__, e__.c_str() ); \
74  action__ \
75  } \
76  catch (char const * e__) \
77  { \
78  THEA_ ## stream__ << format( (message__ + std::string(" (%s)")).c_str(), __VA_ARGS__, e__ ); \
79  action__ \
80  } \
81  catch (...) \
82  { \
83  THEA_ ## stream__ << format( (message__ + std::string(" (Unknown error)")).c_str(), __VA_ARGS__ ); \
84  action__ \
85  }
86 
87 } // namespace Thea
88 
89 #endif
A fatal error class.
Definition: Error.hpp:30
Root namespace for the Thea library.
char const * what() const
The error message.
Definition: Error.hpp:43
FatalError(std::string const &message_)
Constructor.
Definition: Error.hpp:34
~FatalError()
Destructor.
Definition: Error.hpp:37
FatalError & operator=(FatalError const &src)
Assignment operator.
Definition: Error.hpp:40
std::runtime_error Error
An error class.
Definition: Error.hpp:27
This file must be included in every file in the project, before any other include.