00001
00002
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #ifndef __EXCEPTIONS_H
00045 #define __EXCEPTIONS_H
00046
00047 #include <string>
00048 #include <iostream>
00049
00050 #include "consmgr.h"
00051
00052 extern char *__progname;
00053
00054
00055
00056
00057
00058
00059
00063 class CMException : public std::exception {
00064 public:
00065 virtual const char* what() const throw()
00066 { return(_what.c_str()); }
00067
00068 protected:
00069 ~CMException() throw() {}
00070
00071 std::string _what;
00072 };
00073
00078 class pthread_exception : public CMException {
00079 public:
00080 pthread_exception(const char*f, const int l, const int r,
00081 const std::string&m);
00082 ~pthread_exception() throw() {}
00083
00084 const char * what(void) const throw();
00085
00086 private:
00087 std::string description;
00088 std::string filename;
00089 int line_number;
00090 int result_code;
00091 };
00092
00096 #define PTHREAD_CHECK_AND_THROW(rc, str) if ((rc) != 0) throw(pthread_exception(__FILE__, __LINE__, (rc), (str)))
00097
00101 class fireball : public CMException {
00102 public:
00103 fireball(const int exit_val, const std::string &msg,
00104 const bool with_errno=false);
00105 ~fireball() throw() {}
00106
00108 inline void bail(std::ostream &out) const throw() {
00109 out << _what << std::endl;
00110 exit(exit_val);
00111 }
00112
00113 const char * what(void) const throw();
00114
00115 private:
00116 const std::string message;
00117 int exit_val;
00118 bool with_errno;
00119 int saved_errno;
00120 };
00121
00125 class CMInvalidParameter : public CMException {
00126 public:
00127 CMInvalidParameter(const std::string &explanation)
00128 { _what = std::string("Invalid parameter: ") + explanation; }
00129
00130
00131 #if 0
00132 const char * what(void) const throw()
00133 { return(_what.c_str()); }
00134 #endif
00135 };
00136
00141 class CMUnsupported : public CMException {
00142 public:
00143 CMUnsupported(const std::string &explanation)
00144 { _what = std::string("Unsupported operation: ") + explanation; }
00145
00146
00147 #if 0
00148 const char * what(void) const throw()
00149 { return(_what.c_str()); }
00150 #endif
00151 };
00152
00153 class CMStateChange : public CMException {
00154 };
00155
00156 class CMFailure : public CMException {
00157 public:
00158 CMFailure(const std::string &explanation, bool use_errno=false);
00159
00160 private:
00161 int saved_errno;
00162 };
00163
00167 #define CATCH_OR_DIE(ec) catch (pthread_exception &pe) { \
00168 std::cerr << pe.what() << std::endl; \
00169 exit((ec)); \
00170 } catch (fireball &f) { \
00171 f.bail(std::cerr); \
00172 } catch (std::exception &e) { \
00173 std::cerr << "Standard exception: " \
00174 << e.what() << std::endl; \
00175 exit((ec)); \
00176 } catch (...)
00177
00178 #define CATCH_STR_OR_DIE(ec) catch (pthread_exception &pe) { \
00179 std::cerr << pe.what() << std::endl; \
00180 exit((ec)); \
00181 } catch (fireball &f) { \
00182 f.bail(std::cerr); \
00183 } catch (char *s) { \
00184 std::cerr << s << std::endl; \
00185 exit((ec)); \
00186 } catch (string &s) { \
00187 std::cerr << s << std::endl; \
00188 exit((ec)); \
00189 } catch (std::exception &e) { \
00190 std::cerr << "Standard exception: " \
00191 << e.what() << std::endl; \
00192 exit((ec)); \
00193 } catch (...)
00194 #endif