LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Private Attributes | List of all members
lsst::pex.exceptions::Exception Class Reference

#include <Exception.h>

Inheritance diagram for lsst::pex.exceptions::Exception:

Public Member Functions

 Exception (char const *file, int line, char const *func, std::string const &message)
 
 Exception (std::string const &message)
 
virtual ~Exception (void) throw ()
 
void addMessage (char const *file, int line, char const *func, std::string const &message)
 
Traceback const & getTraceback (void) const throw ()
 Retrieve the list of tracepoints associated with an exception. More...
 
virtual std::ostream & addToStream (std::ostream &stream) const
 
virtual char const * what (void) const throw ()
 
virtual char const * getType (void) const throw ()
 
virtual Exceptionclone (void) const
 

Private Attributes

std::string _message
 
Traceback _traceback
 

Detailed Description

Definition at line 99 of file Exception.h.

Constructor & Destructor Documentation

Exception::Exception ( char const *  file,
int  line,
char const *  func,
std::string const &  message 
)

Standard constructor, intended for C++ use via the LSST_EXCEPT() macro.

Parameters
[in]fileFilename (automatically passed in by macro).
[in]lineLine number (automatically passed in by macro).
[in]funcFunction name (automatically passed in by macro).
[in]messageInformational string attached to exception.

Definition at line 44 of file Exception.cc.

47  : _message(message), _traceback(1, Tracepoint(file, line, func, message)) {}
Exception::Exception ( std::string const &  message)
explicit

Message-only constructor, intended for use from Python only.

While this constructor can be called from C++, it's better to use the LSST_EXCEPT macro there, which stores file/line/function information as well. In Python, however, that information is stored outside the exception, so we don't want to duplicate it, and hence this constructor is invoked instead.

Parameters
[in]messageInformational string attached to exception.

Definition at line 49 of file Exception.cc.

51  : _message(message), _traceback() {}
Exception::~Exception ( void  )
throw (
)
virtual

Definition at line 53 of file Exception.cc.

53 {}

Member Function Documentation

void Exception::addMessage ( char const *  file,
int  line,
char const *  func,
std::string const &  message 
)

Add a tracepoint and a message to an exception before rethrowing it (access via LSST_EXCEPT_ADD).

Parameters
[in]fileFilename (automatically passed in by macro).
[in]lineLine number (automatically passed in by macro).
[in]funcFunction name (automatically passed in by macro).
[in]messageAdditional message to associate with this rethrow.

Definition at line 55 of file Exception.cc.

57  {
58  std::ostringstream stream;
59  stream << _message;
60  if (_traceback.empty()) {
61  // This means the message-only constructor was used, which should only happen
62  // from Python...but this method isn't accessible from Python, so maybe
63  // this Exception was thrown in Python, then passed back to C++. Or, more
64  // likely, someone didn't read the warning and threw this exception in C++
65  // without using LSST_EXCEPT.
66  // But, because we don't have a traceback for that first message, we can't
67  // add one here without messing up the stringification logic later. Since
68  // this is a rare case (and should be considered a bug, but we don't want
69  // exception code throwing its own exceptions unless it absolutely has to),
70  // we'll proceed by just appending the message and ignoring the traceback.
71  stream << "; " << message;
72  } else {
73  if (_traceback.size() == static_cast<std::size_t>(1)) {
74  // The original message doesn't have an index (because it's faster,
75  // and there's no need if there's only one), so when we add the second,
76  // we have to give it an index.
77  stream << " {0}; ";
78  } else {
79  stream << "; ";
80  }
81  stream << message << " {" << _traceback.size() << "}";
82  _traceback.push_back(Tracepoint(file, line, func, message));
83  }
84  _message = stream.str();
85 
86 }
std::ostream & Exception::addToStream ( std::ostream &  stream) const
virtual

Add a text representation of this exception, including its traceback with messages, to a stream.

Parameters
[in]streamReference to an output stream.
Returns
Reference to the output stream after adding the text.

Definition at line 93 of file Exception.cc.

93  {
94  if (_traceback.empty()) {
95  // The exception was raised in Python, so we don't include the traceback, the type, or any
96  // newlines, because Python will print those itself.
97  stream << _message;
98  } else {
99  stream << std::endl; // Start with a newline to separate our stuff from Pythons "<type>: " prefix.
100  for (std::size_t i = 0; i != _traceback.size(); ++i) {
101  stream << " File \"" << _traceback[i]._file
102  << "\", line " << _traceback[i]._line
103  << ", in " << _traceback[i]._func << std::endl;
104  stream << " " << _traceback[i]._message << " {" << i << "}" << std::endl;
105  }
106  std::string type(getType(), 0, std::strlen(getType()) - 2);
107  stream << type << ": '" << _message << "'" << std::endl;
108  }
109  return stream;
110 }
virtual char const * getType(void) const
Definition: Exception.cc:116
Exception * Exception::clone ( void  ) const
virtual

Return a copy of the exception as an Exception*. Can be overridden by derived classes that add data or methods.

Returns
Exception* pointing to a copy of the exception.

Definition at line 120 of file Exception.cc.

120  {
121  return new Exception(*this);
122 }
Exception(char const *file, int line, char const *func, std::string const &message)
Definition: Exception.cc:44
Traceback const & Exception::getTraceback ( void  ) const
throw (
)

Retrieve the list of tracepoints associated with an exception.

Definition at line 89 of file Exception.cc.

89  {
90  return _traceback;
91 }
char const * Exception::getType ( void  ) const
throw (
)
virtual

Return the fully-specified C++ type of a pointer to the exception. This is overridden by derived classes (automatically if the LSST_EXCEPTION_TYPE macro is used). It is used by the SWIG interface.

Returns
String with the C++ type; does not need to be freed/deleted.

Definition at line 116 of file Exception.cc.

116  {
117  return "lsst::pex::exceptions::Exception *";
118 }
char const * Exception::what ( void  ) const
throw (
)
virtual

Return a character string summarizing this exception.

This combines all the messages added to the exception, but not the type or traceback (use the stream operator to get this more detailed information).

Not allowed to throw any exceptions.

Returns
String representation; does not need to be freed/deleted.

Definition at line 112 of file Exception.cc.

112  {
113  return _message.c_str();
114 }

Member Data Documentation

std::string lsst::pex.exceptions::Exception::_message
private

Definition at line 174 of file Exception.h.

Traceback lsst::pex.exceptions::Exception::_traceback
private

Definition at line 175 of file Exception.h.


The documentation for this class was generated from the following files: