thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
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>
15#include "Exception.fh"
16#include <string>
17#include <iosfwd>
18
19extern "C" {
20 void breakThePEG();
21}
22
23namespace ThePEG {
24
44class Exception: public exception {
45
46public:
47
51 enum Severity {
53 info,
55 warning,
61 runerror,
68 };
69
70public:
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
99public:
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
129public:
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
180protected:
181
186
190 mutable ostringstream theMessage;
191
192private:
193
197 mutable bool handled;
198
203
207 static ostream * errstream;
208
209public:
210
215 static bool noabort;
216
217};
218
219}
220
221#endif /* ThePEG_Exception_H */
This is the main config header file for ThePEG.
Exception is the base class for all exceptions to be used in ThePEG.
Definition: Exception.h:44
bool operator==(const Exception &ex) const
Comparison.
Definition: Exception.h:115
Exception & operator<<(const T &t)
Add info to the exception message.
Definition: Exception.h:167
void writeMessage(ostream &os= *errstream) const
Write the error message to a stream.
Severity theSeverity
The severity.
Definition: Exception.h:202
ostringstream theMessage
Stream to write the error message to.
Definition: Exception.h:190
Exception & operator<<(Severity sev)
Set the severity for the exception.
Definition: Exception.h:175
void severity(Severity)
set the severity.
Severity severity() const
Return the severity.
Definition: Exception.h:156
static ostream * errstream
The default stream to write the error message if unhandled.
Definition: Exception.h:207
virtual const char * what() const noexcept
Return the error message.
Definition: Exception.h:134
string message() const
Return the error message.
Definition: Exception.h:143
Exception()
Default constructor.
Definition: Exception.h:82
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
@ warning
Possibly severe, (the user should be warned).
Definition: Exception.h:55
@ eventerror
Possibly severe, (the event being generated should be discarded).
Definition: Exception.h:59
@ maybeabort
Severe error, (the run should be terminated, possibly dumping core).
Definition: Exception.h:63
@ info
Not severe (but the user should be informed).
Definition: Exception.h:53
@ unknown
Unknown severity.
Definition: Exception.h:52
@ abortnow
Severe error, (the run is aborted immediately, before the exception is thrown).
Definition: Exception.h:65
@ runerror
Severe error, (the run should be terminated).
Definition: Exception.h:61
@ setuperror
Command failed during setup phase, execution is continued.
Definition: Exception.h:57
Exception(const Exception &ex)
The copy constructor.
Definition: Exception.h:87
bool operator<(const Exception &ex) const
Compare severity.
Definition: Exception.h:123
static bool noabort
If this flag is set, all abortnow and maybeabort severities will be treated as runerror.
Definition: Exception.h:215
Exception(const string &str, Severity sev)
Standard constructor.
virtual ~Exception() noexcept
The destructor.
bool handled
True if this exception has been taken care of.
Definition: Exception.h:197
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
STL namespace.