Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Exception.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2001 Carnegie Mellon University.  All Rights Reserved.
00003  *
00004  * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
00005  * is subject to the terms of the software license set forth in the LICENSE
00006  * file included with this software, and also available at
00007  * http://www.lemurproject.org/license.html
00008  *
00009  *==========================================================================
00010 */
00011 
00012 
00013 #ifndef _EXCEPTION_HPP
00014 #define _EXCEPTION_HPP
00015 
00017 
00063 #include <iostream>
00064 #include <string>
00065 #include <sstream>
00066 using namespace std;
00067 
00068 typedef unsigned long LemurErrorType;
00069 
00070 class Exception {
00071 public:
00072   Exception(char *throwerString=" unknown thrower", char *whatString="unknown exception") {
00073     _what = throwerString;
00074     _what += ": ";
00075     _what += whatString;
00076   }
00077 
00078   Exception( const std::string& whoString, int whereLine, 
00079              const std::string& whatString, LemurErrorType code )
00080   {
00081     std::stringstream lineString;
00082     lineString << whereLine;
00083 
00084     _what = whoString + "(" + lineString.str() + ")" + ": " + whatString;
00085     _code = code;
00086   }
00087 
00088   Exception( const std::string& whoString, int whereLine,
00089              const std::string& whatString, const Exception& inner )
00090   {
00091     std::stringstream lineString;
00092     lineString << whereLine;
00093 
00094      _what = whoString + "(" + lineString.str() + "): " + whatString + "\n\t" + inner.what();
00095     _code = inner.code();
00096   }
00097 
00098   ~Exception() {}
00099 
00100   inline void writeMessage(std::ostream &os = std::cerr)
00101   {
00102     os << "Exception [code = " << _code << "]" << std::endl << _what << std::endl;
00103   }
00104 
00105   const std::string& what() const {
00106     return _what;
00107   }
00108 
00109   LemurErrorType code() const {
00110     return _code;
00111   }
00112 
00113 private:
00114   std::string _what;
00115   LemurErrorType _code;
00116 };
00117 
00118 #define LEMUR_ABORT( e )                  { std::cerr << e.what() << std::endl; exit(-1); }
00119 #define LEMUR_THROW_LINE( code, text, file, line )  throw Exception( file, line, std::string() + text, (code) )
00120 #define LEMUR_THROW(code, text)  LEMUR_THROW_LINE(code, text, __FILE__, __LINE__)
00121 #define LEMUR_RETHROW_LINE( e, text, file, line )   throw Exception( file, line, (std::string() + text), (e) )
00122 #define LEMUR_RETHROW( e, text)  LEMUR_RETHROW_LINE(e, text, __FILE__, __LINE__)
00123 
00124 #define LEMUR_GENERIC_ERROR               ((LemurErrorType)0xFFFFFFFF)
00125 #define LEMUR_MISSING_PARAMETER_ERROR     ((LemurErrorType)0xFFFFFFFE)
00126 #define LEMUR_BAD_PARAMETER_ERROR         ((LemurErrorType)0xFFFFFFF7)
00127 #define LEMUR_PARSE_ERROR                 ((LemurErrorType)0xFFFFFFFD)
00128 #define LEMUR_KEYFILE_IO_ERROR            ((LemurErrorType)0xFFFFFFFC)
00129 #define LEMUR_IO_ERROR                    ((LemurErrorType)0xFFFFFFFB)
00130 #define LEMUR_RUNTIME_ERROR               ((LemurErrorType)0xFFFFFFFA)
00131 #define LEMUR_NETWORK_ERROR               ((LemurErrorType)0xFFFFFFF9)
00132 #define LEMUR_INTERNAL_ERROR              ((LemurErrorType)0xFFFFFFF8)
00133 
00134 #endif

Generated on Wed Nov 3 12:58:54 2004 for Lemur Toolkit by doxygen1.2.18