thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
Exception.h
1 // -*- C++ -*-
2 //
3 // Exception.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2019 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef ThePEG_Exception_H
10 #define ThePEG_Exception_H
11 // This is the declaration of the Exception class.
12 
13 #include <exception>
14 #include "ThePEG/Config/ThePEG.h"
15 #include "Exception.fh"
16 #include <string>
17 #include <iosfwd>
18 
19 extern "C" {
20  void breakThePEG();
21 }
22 
23 namespace ThePEG {
24 
44 class Exception: public exception {
45 
46 public:
47 
51  enum Severity {
53  info,
68  };
69 
70 public:
71 
77  Exception(const string & str, Severity sev);
78 
82  Exception() : handled(false), theSeverity(unknown) { breakThePEG(); }
83 
87  Exception(const Exception & ex)
88  : std::exception(ex), theMessage(ex.message()),
90  {
91  ex.handle();
92  }
93 
97  virtual ~Exception() noexcept;
98 
99 public:
100 
104  const Exception & operator=(const Exception & ex) {
105  handled = ex.handled;
106  theMessage << ex.message();
107  theSeverity = ex.severity();
108  ex.handle();
109  return *this;
110  }
111 
115  bool operator==(const Exception & ex) const {
116  return ( message() == ex.message() && severity() == ex.severity() );
117  }
118 
123  bool operator<(const Exception & ex) const {
124  return ( severity() == ex.severity() ?
125  ( message() < ex.message() ) :
126  ( severity() < ex.severity() ) );
127  }
128 
129 public:
130 
134  virtual const char* what() const noexcept {
135  static string str;
136  str = message();
137  return str.c_str();
138  }
139 
143  string message() const {
144  string mess = theMessage.str();
145  return mess.empty() ? string("Error message not provided.") : mess;
146  }
147 
151  void writeMessage(ostream & os = *errstream) const;
152 
156  Severity severity() const { return theSeverity; }
157 
161  void handle() const { handled = true; }
162 
166  template <typename T>
167  Exception & operator<<(const T & t) {
168  theMessage << t;
169  return *this;
170  }
171 
176  severity(sev);
177  return *this;
178  }
179 
180 protected:
181 
185  void severity(Severity);
186 
190  mutable ostringstream theMessage;
191 
192 private:
193 
197  mutable bool handled;
198 
203 
207  static ostream * errstream;
208 
209 public:
210 
215  static bool noabort;
216 
217 };
218 
219 }
220 
221 #endif /* ThePEG_Exception_H */
Exception(const Exception &ex)
The copy constructor.
Definition: Exception.h:87
Exception & operator<<(Severity sev)
Set the severity for the exception.
Definition: Exception.h:175
bool operator==(const Exception &ex) const
Comparison.
Definition: Exception.h:115
bool handled
True if this exception has been taken care of.
Definition: Exception.h:197
const Exception & operator=(const Exception &ex)
Assignment.
Definition: Exception.h:104
STL namespace.
static bool noabort
If this flag is set, all abortnow and maybeabort severities will be treated as runerror.
Definition: Exception.h:215
Exception()
Default constructor.
Definition: Exception.h:82
static ostream * errstream
The default stream to write the error message if unhandled.
Definition: Exception.h:207
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
This is the main config header file for ThePEG.
virtual const char * what() const noexcept
Return the error message.
Definition: Exception.h:134
Command failed during setup phase, execution is continued.
Definition: Exception.h:57
void writeMessage(ostream &os= *errstream) const
Write the error message to a stream.
Exception & operator<<(const T &t)
Add info to the exception message.
Definition: Exception.h:167
bool operator<(const Exception &ex) const
Compare severity.
Definition: Exception.h:123
Severity theSeverity
The severity.
Definition: Exception.h:202
Severe error, (the run should be terminated).
Definition: Exception.h:61
Not severe (but the user should be informed).
Definition: Exception.h:53
Severity severity() const
Return the severity.
Definition: Exception.h:156
void handle() const
Indicate that this exception has been taken care of.
Definition: Exception.h:161
Severity
The levels of severity.
Definition: Exception.h:51
ostringstream theMessage
Stream to write the error message to.
Definition: Exception.h:190
Severe error, (the run should be terminated, possibly dumping core).
Definition: Exception.h:63
Exception is the base class for all exceptions to be used in ThePEG.
Definition: Exception.h:44
Possibly severe, (the user should be warned).
Definition: Exception.h:55
string message() const
Return the error message.
Definition: Exception.h:143
Possibly severe, (the event being generated should be discarded).
Definition: Exception.h:59
Unknown severity.
Definition: Exception.h:52
virtual ~Exception() noexcept
The destructor.
Severe error, (the run is aborted immediately, before the exception is thrown).
Definition: Exception.h:65