All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GaudiException.h
Go to the documentation of this file.
1 // $Id: GaudiException.h,v 1.6 2007/12/20 18:28:33 marcocle Exp $
2 #ifndef GAUDIKERNEL_GAUDIEXCEPTION_H
3 #define GAUDIKERNEL_GAUDIEXCEPTION_H
4 
5 // Include files
6 #include "GaudiKernel/Kernel.h"
9 #include "GaudiKernel/System.h"
10 
11 #include <string>
12 #include <iostream>
13 #include <exception>
14 
15 
24 class GAUDI_API GaudiException: virtual public std::exception {
25  // friends
26  friend inline std::ostream& operator<< ( std::ostream& os ,
27  const GaudiException& ge ) ;
28  friend inline std::ostream& operator<< ( std::ostream& os ,
29  const GaudiException* pge ) ;
30  friend inline MsgStream& operator<< ( MsgStream& os ,
31  const GaudiException& ge ) ;
32  friend inline MsgStream& operator<< ( MsgStream& os ,
33  const GaudiException* pge ) ;
34  friend class StatusCode;
35 
36 public:
42  GaudiException( const std::string& Message,
43  const std::string& Tag,
44  const StatusCode & Code )
45  : m_message ( Message )
46  , m_tag ( Tag )
47  , m_code ( Code )
48  , m_previous ( 0 )
49  { s_proc = true; }
50 
57  GaudiException( const std::string& Message ,
58  const std::string& Tag ,
59  const StatusCode & Code ,
60  const GaudiException& Exception )
61  : m_message ( Message )
62  , m_tag ( Tag )
63  , m_code ( Code )
64  , m_previous ( Exception.clone() )
65  {}
66 
73  GaudiException( const std::string& Message ,
74  const std::string& Tag ,
75  const StatusCode & Code ,
76  const std::exception& Exception )
77  : m_message ( Message )
78  , m_tag ( Tag )
79  , m_code ( Code )
80  , m_previous ( 0 )
81  {
82  s_proc = true;
83  m_message += ": " + System::typeinfoName(typeid(Exception)) + ", " +
84  Exception.what();
85  }
86 
88  GaudiException( const GaudiException& Exception ) : std::exception(Exception)
89  {
90  s_proc = true;
91  m_message = Exception.message() ;
92  m_tag = Exception.tag () ;
93  m_code = Exception.code () ;
94  m_previous = ( 0 == Exception.previous() ) ?
95  0 : Exception.previous()->clone() ;
96  }
97 
99  virtual ~GaudiException() throw() {
100  m_code.setChecked();
101  if( 0 != m_previous ) { delete m_previous ; m_previous = 0 ; }
102  s_proc = false;
103  }
104 
106  GaudiException& operator=( const GaudiException& Exception ) {
107  if ( &Exception == this ) { return *this; }
108  m_message = Exception.message() ;
109  m_tag = Exception.tag () ;
110  m_code = Exception.code () ;
111  if( 0 != m_previous ) { delete m_previous; m_previous = 0 ; }
112  m_previous = ( 0 == Exception.previous() ) ?
113  0 : Exception.previous()->clone() ;
114  return *this;
115  }
116 
118  virtual const std::string& message () const { return m_message; }
119 
121  virtual const std::string& setMessage( const std::string& newMessage ) {
122  m_message = newMessage; return message() ;
123  }
124 
126  virtual const std::string& tag () const { return m_tag; }
127 
129  virtual const std::string& setTag ( const std::string& newTag ) {
130  m_tag = newTag ; return tag() ;
131  }
132 
134  virtual const StatusCode& code () const { return m_code; }
135 
137  virtual const StatusCode& setCode ( const StatusCode& newStatus ) {
138  m_code = newStatus; return code() ;
139  }
140 
142  virtual GaudiException* previous () const { return m_previous ; }
143 
145  virtual std::ostream& printOut ( std::ostream& os = std::cerr ) const {
146  os << tag() << " \t " << message() ;
147  switch( code() ) {
148  case StatusCode::SUCCESS : os << "\t StatusCode=SUCCESS" ; break ;
149  case StatusCode::FAILURE : os << "\t StatusCode=FAILURE" ; break ;
150  default : os << "\t StatusCode=" << code() ; break ;
151  }
152  return ( 0 != previous() ) ? previous()->printOut( os << std::endl ) : os ;
153  }
154 
156  virtual MsgStream& printOut ( MsgStream& os ) const {
157  os << tag() << "\t" << message() ;
158  switch( code() ) {
159  case StatusCode::SUCCESS : os << "\t StatusCode=SUCCESS" ; break ;
160  case StatusCode::FAILURE : os << "\t StatusCode=FAILURE" ; break ;
161  default : os << "\t StatusCode=" << code().getCode() ; break ;
162  }
163  return ( 0 != previous() ) ? previous()->printOut( os << endmsg ) : os ;
164  }
165 
167  virtual GaudiException* clone() const { return new GaudiException(*this); }
168 
170  virtual const char* what () const throw() { return message().c_str() ; }
171 protected:
172  mutable std::string m_message ;
173  mutable std::string m_tag ;
174  mutable StatusCode m_code ;
176  static bool s_proc;
177 };
178 
180 std::ostream& operator<< ( std::ostream& os , const GaudiException& ge ) {
181  return ge.printOut( os );
182 }
183 std::ostream& operator<< ( std::ostream& os , const GaudiException* pge )
184 { return (0 == pge) ?
185  ( os << " GaudiException* points to NULL!" ) : ( os << *pge ); }
186 
189  return ge.printOut( os );
190 }
193  return (0 == pge) ?
194  ( os << " GaudiException* points to NULL!" ) : ( os << *pge );
195 }
196 
197 #endif // GAUDIKERNEL_GAUDIEXCEPTION_H
virtual const std::string & message() const
error message to be printed
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Define general base for Gaudi exception.
virtual const StatusCode & setCode(const StatusCode &newStatus)
update the status code for the exception
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
GaudiException & operator=(const GaudiException &Exception)
assignment operator
static bool s_proc
"previous" element in the linked list
virtual const std::string & setMessage(const std::string &newMessage)
update the error message to be printed
std::ostream & operator<<(std::ostream &os, const GaudiException &ge)
overloaded printout to std::ostream
GaudiException(const std::string &Message, const std::string &Tag, const StatusCode &Code, const std::exception &Exception)
Constructor (3)
std::string m_tag
error message
virtual const StatusCode & code() const
StatusCode for Exception.
std::string m_message
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
virtual GaudiException * previous() const
get the previous exception ( "previous" element in the linked list)
virtual GaudiException * clone() const
clone operation
virtual const std::string & tag() const
name tag for the exception, or exception type
virtual const char * what() const
method from std::exception
GaudiException(const std::string &Message, const std::string &Tag, const StatusCode &Code)
Constructor (1)
virtual MsgStream & printOut(MsgStream &os) const
Output the exception to the Gaudi MsgStream.
The Message class.
Definition: Message.h:15
virtual const std::string & setTag(const std::string &newTag)
update name tag
GaudiException * m_previous
status code for exception
virtual std::ostream & printOut(std::ostream &os=std::cerr) const
methods for overloaded printout to std::ostream& and MsgStream&
virtual ~GaudiException()
destructor (perform the deletion of "previous" field!)
StatusCode m_code
exception tag
GaudiException(const std::string &Message, const std::string &Tag, const StatusCode &Code, const GaudiException &Exception)
Constructor (2)
#define GAUDI_API
Definition: Kernel.h:108
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
GaudiException(const GaudiException &Exception)
Copy constructor (deep copying!)