1#ifndef _DRAMA2_EXCEPT_H
2#define _DRAMA2_EXCEPT_H
43#include "drama2_err.h"
66#define DramaTHROW(status_,message_) throw drama::Exception(__func__, __FILE__,__LINE__,(message_), (status_))
83#define DramaTHROW_S(status_,format_,...) drama::ExceptionThrowSafe(__func__, __FILE__,__LINE__,(status_), (format_), __VA_ARGS__)
137 class Exception :
public std::exception {
144 std::string _sourceFile;
145 std::string _sourceFunc;
146 std::string _origMessage;
148 std::ostringstream _message;
149 std::ostringstream _stackTrace;
150 static const unsigned _stackTraceMax = 25;
151 std::string _whatString;
153 static bool _firstReportDone;
156 bool _alwaysStacktrace =
false;
163 void GenerateStackTrace(
const int nSize,
void *array[]);
176 Exception() : _lineNum(0), _sourceFile(
"none"),
177 _sourceFunc(
"none"), _origMessage(
"none"),
200 const std::string &
file,
202 const std::string &
message =
"",
224 const std::string
func,
225 const std::string &
file,
228 const char *
format, ...) :
247 _lineNum(
source._lineNum),
248 _sourceFile(
source._sourceFile),
250 _origMessage(
source._origMessage),
251 _dramaStatus(
source._dramaStatus),
252 _whatString(
source._whatString),
253 _alwaysStacktrace(
source._alwaysStacktrace) {
254 _message.str(
source._message.str());
262 _lineNum(std::move(
source._lineNum)),
263 _sourceFile(std::move(
source._sourceFile)),
264 _sourceFunc(std::move(
source._sourceFunc)),
265 _origMessage(std::move(
source._origMessage)),
266 _dramaStatus(std::move(
source._dramaStatus)),
267 _whatString(std::move(
source._whatString)),
268 _alwaysStacktrace(
source._alwaysStacktrace) {
275 _message.str(
source._message.str());
276 _stackTrace.str(
source._stackTrace.str());
290 this->_sourceFile =
rhs._sourceFile;
291 this->_sourceFunc =
rhs._sourceFunc;
292 this->_lineNum =
rhs._lineNum;
293 this->_origMessage =
rhs._origMessage;
294 this->_dramaStatus =
rhs._dramaStatus;
295 this->_whatString =
rhs._whatString;
297 this->_message.str(
rhs._message.str());
298 this->_stackTrace.str(
rhs._stackTrace.str());
299 this->_alwaysStacktrace =
rhs._alwaysStacktrace;
312 this->_sourceFile = std::move(
rhs._sourceFile);
313 this->_sourceFunc = std::move(
rhs._sourceFunc);
314 this->_lineNum = std::move(
rhs._lineNum);
315 this->_origMessage = std::move(
rhs._origMessage);
316 this->_dramaStatus = std::move(
rhs._dramaStatus);
317 this->_whatString = std::move(
rhs._whatString);
322 this->_message.str(
rhs._message.str());
323 this->_stackTrace.str(
rhs._stackTrace.str());
325 this->_alwaysStacktrace =
rhs._alwaysStacktrace;
338 virtual std::string
toString()
const;
359 virtual const std::string &
fileName()
const {
367 virtual const std::string &
function()
const {
430 return _stackTrace.str();
464 virtual void ErsOut()
const;
482 return _whatString.c_str();
503 template<
typename T,
typename...
Types>
525 "extra arguments provided to drama::ExceptionThrowSafe");
554 template<
typename...
Types>
556 const std::string &
file,
588 std::ios::fmtflags oldFlags = strm.flags();
591 <<
"DRAMA Status = 0x"
597 strm.flags(oldFlags);
D2DEPRECATED Exception(bool makeUnique, const std::string func, const std::string &file, const int lineNum, const StatusType status, const char *format,...)
Create an exception using C printf() style string formatting.
Definition exception.hh:249
virtual std::string getStackTrace() const
Return the stack trace created when the exception was created.
Definition exception.hh:456
virtual int lineNumber() const
Returns the source file line number at which the exception occurred.
Definition exception.hh:378
virtual int statusAsSysExitCode() const
Return a system exit code based on the DRAMA Status.
Definition exception.hh:424
virtual void ErsRep() const
Report details of this exception using ERS.
virtual void ErsOut() const
Output a DRAMA ERS report about this exception.
Exception(const std::string func, const std::string &file, const int lineNum, const std::string &message="", const StatusType status=DRAMA2__CPP_ERROR, const bool alwaysStacktrace=false)
Create an Exception.
virtual std::string toString() const
Return the exception string.
virtual const std::string & fileName() const
Returns the name of the C++ source file in which the exception occurred.
Definition exception.hh:386
virtual const std::string & getOrigMessage() const
Returns the exception original message.
Definition exception.hh:371
const char * what() const noexcept override
Get string identifying exception.
Definition exception.hh:508
virtual void Print(FILE *file=stderr, bool include_stacktrace=false) const
Print exception details to a file.
Exception(const Exception &source)
Copy constructor.
Definition exception.hh:273
Exception & operator=(const Exception &rhs)
Assignment operator.
Definition exception.hh:312
void FmtWrite(const char *format, T value, Types... args)
Safe formated write to an exception message string.
Definition exception.hh:531
void FmtWrite(const char *str)
Safe formatted write to an exception message string.
virtual const std::string & function() const
Returns the name of the function in which the exception occurred.
Definition exception.hh:394
void AddErs()
Add the any errors from the current ERS context to the exception.
virtual std::string dramaStatusStr() const
Return the DRAMA Status code converted to a DRAMA.
Exception & operator<<(const T &t)
Add extra information to an exception message.
Definition exception.hh:445
Exception(Exception &&source) noexcept
Move constructor.
Definition exception.hh:288
Exception()
Create an Exception, details empty.
Definition exception.hh:203
virtual StatusType dramaStatus() const
Return the DRAMA Status code associated with the exception.
Definition exception.hh:404
virtual ~Exception()
Virtual Destructor - since we will be inherited.
Definition exception.hh:557
Exception & operator=(Exception &&rhs) noexcept
Move assignment operator.
Definition exception.hh:334
An Exception class for exceptions thrown by DRAMA V2 classes.
Definition exception.hh:164
std::ostream & operator<<(std::ostream &strm, const drama::Exception &e)
drama::Exception stream output operator
Definition exception.hh:613
#define DramaTHROW(status_, message_)
Throw a Drama exception.
Definition exception.hh:93
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1322
void ExceptionThrowSafe(const std::string func, const std::string &file, const int lineNum, StatusType status, const char *format, Types... args)
Create and throw an exception, variable argument list, Safe formatting of arguments.
Definition exception.hh:582
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition drama.hh:93
DRAMA 2 include file - Utility macros, types etc.