1#ifndef _DRAMA2_EXCEPT_H
2#define _DRAMA2_EXCEPT_H
37#include "drama2_err.h"
60#define DramaTHROW(status_,message_) throw drama::Exception(__func__, __FILE__,__LINE__,(message_), (status_))
81#define DramaTHROW_S(status_,format_,...) drama::ExceptionThrowSafe(__func__, __FILE__,__LINE__,(status_), (format_), __VA_ARGS__)
135 class Exception :
public std::exception {
142 std::string _sourceFile;
143 std::string _sourceFunc;
144 std::string _origMessage;
146 std::ostringstream _message;
147 std::ostringstream _stackTrace;
148 static const unsigned _stackTraceMax = 25;
149 std::string _whatString;
151 static bool _firstReportDone;
154 bool _alwaysStacktrace =
false;
161 void GenerateStackTrace(
const int nSize,
void *array[]);
174 Exception() : _lineNum(0), _sourceFile(
"none"),
175 _sourceFunc(
"none"), _origMessage(
"none"),
198 const std::string &
file,
200 const std::string &
message =
"",
222 const std::string
func,
223 const std::string &
file,
226 const char *
format, ...) :
245 _lineNum(
source._lineNum),
246 _sourceFile(
source._sourceFile),
248 _origMessage(
source._origMessage),
249 _dramaStatus(
source._dramaStatus),
250 _whatString(
source._whatString),
251 _alwaysStacktrace(
source._alwaysStacktrace) {
252 _message.str(
source._message.str());
260 _lineNum(std::move(
source._lineNum)),
261 _sourceFile(std::move(
source._sourceFile)),
262 _sourceFunc(std::move(
source._sourceFunc)),
263 _origMessage(std::move(
source._origMessage)),
264 _dramaStatus(std::move(
source._dramaStatus)),
265 _whatString(std::move(
source._whatString)),
266 _alwaysStacktrace(
source._alwaysStacktrace) {
273 _message.str(
source._message.str());
274 _stackTrace.str(
source._stackTrace.str());
288 this->_sourceFile =
rhs._sourceFile;
289 this->_sourceFunc =
rhs._sourceFunc;
290 this->_lineNum =
rhs._lineNum;
291 this->_origMessage =
rhs._origMessage;
292 this->_dramaStatus =
rhs._dramaStatus;
293 this->_whatString =
rhs._whatString;
295 this->_message.str(
rhs._message.str());
296 this->_stackTrace.str(
rhs._stackTrace.str());
297 this->_alwaysStacktrace =
rhs._alwaysStacktrace;
310 this->_sourceFile = std::move(
rhs._sourceFile);
311 this->_sourceFunc = std::move(
rhs._sourceFunc);
312 this->_lineNum = std::move(
rhs._lineNum);
313 this->_origMessage = std::move(
rhs._origMessage);
314 this->_dramaStatus = std::move(
rhs._dramaStatus);
315 this->_whatString = std::move(
rhs._whatString);
320 this->_message.str(
rhs._message.str());
321 this->_stackTrace.str(
rhs._stackTrace.str());
323 this->_alwaysStacktrace =
rhs._alwaysStacktrace;
360 virtual const std::string &
fileName()
const {
368 virtual const std::string &
function()
const {
431 return _stackTrace.str();
465 virtual void ErsOut()
const;
483 return _whatString.c_str();
504 template<
typename T,
typename...
Types>
526 "extra arguments provided to drama::ExceptionThrowSafe");
555 template<
typename...
Types>
557 const std::string &
file,
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:247
virtual std::string getStackTrace() const
Return the stack trace created when the exception was created.
Definition exception.hh:457
virtual int lineNumber() const
Returns the source file line number at which the exception occurred.
Definition exception.hh:379
virtual int statusAsSysExitCode() const
Return a system exit code based on the DRAMA Status.
Definition exception.hh:425
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 const std::string & fileName() const
Returns the name of the C++ source file in which the exception occurred.
Definition exception.hh:387
virtual const std::string & getOrigMessage() const
Returns the exception original message.
Definition exception.hh:372
const char * what() const noexcept override
Get string identifying exception.
Definition exception.hh:509
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:271
Exception & operator=(const Exception &rhs)
Assignment operator.
Definition exception.hh:310
void FmtWrite(const char *format, T value, Types... args)
Safe formated write to an exception message string.
Definition exception.hh:532
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:395
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.
virtual std::string toString(bool details=false) const
Return the exception string.
Exception & operator<<(const T &t)
Add extra information to an exception message.
Definition exception.hh:446
Exception(Exception &&source) noexcept
Move constructor.
Definition exception.hh:286
Exception()
Create an Exception, details empty.
Definition exception.hh:201
virtual StatusType dramaStatus() const
Return the DRAMA Status code associated with the exception.
Definition exception.hh:405
virtual ~Exception()
Virtual Destructor - since we will be inherited.
Definition exception.hh:558
Exception & operator=(Exception &&rhs) noexcept
Move assignment operator.
Definition exception.hh:332
An Exception class for exceptions thrown by DRAMA V2 classes.
Definition exception.hh:162
std::ostream & operator<<(std::ostream &strm, const drama::Exception &e)
drama::Exception stream output operator
Definition exception.hh:614
#define DramaTHROW(status_, message_)
Throw a Drama exception.
Definition exception.hh:87
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:583
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.