CARMA C++
ErrorException.h
Go to the documentation of this file.
1 #ifndef CARMA_UTIL_ERROR_EXCEPTION_H
2 #define CARMA_UTIL_ERROR_EXCEPTION_H
3 
38 #include <string>
39 #include <iosfwd>
40 
41 #include <log4cpp/Priority.hh>
42 
44 
45 
46 namespace carma {
47  namespace util {
48 
54 public:
55 
64  ErrorException(const std::string& msg, const char* filename, int lineNo);
65 
66 
78  ErrorException(const std::ostringstream& msg,
79  const char* filename, int lineNo);
80 
81 
87  ErrorException(const ErrorException& errorException);
88 
92  virtual ~ErrorException() throw();
93 
94 
95  ErrorException & operator=( const ErrorException & rhs );
96 
101  virtual const char* what() const throw();
102 
103 
104  virtual ::std::string getLogString( ) const;
105 
106 
112  void report() const;
113 
118  void log(log4cpp::Priority::PriorityLevel priority) const;
119 
124  std::string getErrorMessage() const ;
125 
126 protected:
130  explicit ErrorException( );
131 
132 private:
133  void swap( ErrorException & rhs );
134 
135  void buildErrorMsg( const char * filename );
136 
137  const char * errorMsg_; // Full message w/line# and file name
138 };
139 
140 } } // End namespace carma::util
141 
142 
163 ::std::ostream & operator<<( ::std::ostream & os,
164  const ::std::exception & error );
165 
166 
171 #define CARMA_ERROR(y) CARMA_EXCEPTION(carma::util::ErrorException,(y))
172 
173 // Macros added by EML to allow error messages to be contructed inline
174 
175 #define ThrowCarmaError(text) \
176 {\
177  std::ostringstream _macroOs; \
178  _macroOs << text;\
179  throw CARMA_ERROR(_macroOs.str());\
180 }
181 
182 #define ThrowCarmaUserException(text) \
183 {\
184  std::ostringstream _macroOs; \
185  _macroOs << text;\
186  throw CARMA_EXCEPTION( carma::util::UserException, _macroOs.str().c_str() );\
187 }
188 
189 #define CARMA_ASSERT( assertion ) \
190  do { \
191  if ( !(assertion) ) \
192  throw CARMA_ERROR( #assertion ); \
193  } while ( false )
194 
195 // ADB: Throw an arbitrary exception ala Erik's methods above.
196 // Note that I 'swallow the semicolon' with the do while loop
197 // to protect against a common macroism. Google it for details.
198 #define ThrowCarmaException(exception, text) \
199  do { \
200  std::ostringstream _macroOs; \
201  _macroOs << text; \
202  throw CARMA_EXCEPTION( exception, _macroOs.str() ); \
203  } while ( false )
204 
205 // ADB: Create a typed derivative error exception
206 // sans boilerplate. Note the derived exception will
207 // live in the namespace this macro is called in.
208 #define MAKE_DERIVED_ERROR_EXCEPTION(T) \
209  \
210 class T : public carma::util::ErrorException { \
211 public: \
212  \
213  T( const std::string & os, \
214  const char * filename, \
215  const int lineNumber) : \
216  carma::util::ErrorException( \
217  ( std::string )( #T ) + ": " + os, \
218  filename, \
219  lineNumber) \
220  { }; \
221  \
222  T( const std::ostringstream & os, \
223  const char * filename, \
224  const int lineNumber) : \
225  carma::util::ErrorException( \
226  ( std::string )( #T ) + ": " + os.str(),\
227  filename, \
228  lineNumber ) \
229  { }; \
230  \
231  virtual ~T() throw () \
232  { }; \
233  \
234 };
235 
236 #endif // CARMA_UTIL_ERROR_EXCEPTION_H
std::ostream & operator<<(::std::ostream &os, const carma::dbms::Table &table)
This is the include file for Carma exception handling utilities.
virtual const char * what() const
Get the error message; overrides BaseException &amp; std::exception.what()
Base exception class for managing carma errors.
Definition: BaseException.h:48
void log(log4cpp::Priority::PriorityLevel priority) const
Log the exception.
std::string getErrorMessage() const
Get the full error message, including line number and file name.
virtual ~ErrorException()
Destructor.
Exception class for errors The exception comes with a text string that can be printed or logged...
void report() const
Report error to standard err.
ErrorException()
Default constructor.