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

Err.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // Copyright (C) 2000
00004 // Ralf Westram
00005 // Time-stamp: <Fri Nov/29/2002 12:46 MET Coder@ReallySoft.de>
00006 //
00007 // Permission to use, copy, modify, distribute and sell this software
00008 // and its documentation for any purpose is hereby granted without fee,
00009 // provided that the above copyright notice appear in all copies and
00010 // that both that copyright notice and this permission notice appear
00011 // in supporting documentation.  Ralf Westram makes no
00012 // representations about the suitability of this software for any
00013 // purpose.  It is provided "as is" without express or implied warranty.
00014 //
00015 // This code is part of my library.
00016 // You may find a more recent version at http://www.reallysoft.de/
00017 //
00019 
00020 #include <Err.h>
00021 #include <Tools.h>
00022 #include <cerrno>
00023 
00024 using namespace std;
00025 using namespace rs;
00026 using namespace rs::str;
00027 using namespace rs::err;
00028 
00029 string Error::module_name = "<unknown>";
00030 
00031 #if defined(DUMP)
00032 // #define DUMP_ERRORS
00033 #endif // DUMP
00034 
00035 // --------------------------------------------------------
00036 //      Error::Error(bool real_error_, const string& s)
00037 // --------------------------------------------------------
00038 Error::Error(bool real_error_, const string& s)
00039     : runtime_error(s)
00040     , error_module(module_name)
00041     , real_error(real_error_)
00042 {
00043 #if defined(DUMP_ERRORS)
00044     cout << "Error(" << s << ")\n";
00045 #endif // DUMP_ERRORS
00046 }
00047 // --------------------------------------
00048 //      Error::Error(const string& s)
00049 // --------------------------------------
00050 Error::Error(const string& s)
00051     : runtime_error(s)
00052     , error_module(module_name)
00053     , real_error(true)
00054 {
00055 #if defined(DUMP_ERRORS)
00056     cout << "Error(" << s << ")\n";
00057 #endif // DUMP_ERRORS
00058 }
00059 // ------------------------------------------------------
00060 //      Error::Error(bool real_error_, const char *s)
00061 // ------------------------------------------------------
00062 Error::Error(bool real_error_, const char *s)
00063     : runtime_error(s)
00064     , error_module(module_name)
00065     , real_error(real_error_)
00066 {
00067 #if defined(DUMP_ERRORS)
00068     cout << "Error(" << s << ")\n";
00069 #endif // DUMP_ERRORS
00070 }
00071 // ------------------------------------------------------
00072 //      Error::Error(bool real_error_, const char *s)
00073 // ------------------------------------------------------
00074 Error::Error(const char *s)
00075     : runtime_error(s)
00076     , error_module(module_name)
00077     , real_error(true)
00078 {
00079 #if defined(DUMP_ERRORS)
00080     cout << "Error(" << s << ")\n";
00081 #endif // DUMP_ERRORS
00082 }
00083 
00084 namespace rs {
00085     namespace err {
00086         //  -----------------------------------------
00087         //      void reThrow(const exception& e)
00088         //  -----------------------------------------
00089         void reThrow(const exception& e) {
00090             const Error *is_err = dynamic_cast<const Error*>(&e);
00091             if (is_err) throw *is_err;
00092 
00093             const char *kind_of = "exception";
00094             const logic_error *is_logic = dynamic_cast<const logic_error*>(&e);
00095             if (is_logic) {
00096                 kind_of = "logic_error";
00097                 const domain_error *is_domain = dynamic_cast<const domain_error*>(is_logic);
00098                 if (is_domain) {
00099                     kind_of = "domain_error";
00100                 }
00101                 else {
00102                     const invalid_argument *is_inv_arg = dynamic_cast<const invalid_argument*>(is_logic);
00103                     if (is_inv_arg) {
00104                         kind_of = "invalid_argument";
00105                     }
00106                     else {
00107                         const length_error *is_length = dynamic_cast<const length_error*>(is_logic);
00108                         if (is_length) {
00109                             kind_of = "length_error";
00110                         }
00111                         else {
00112                             const out_of_range *is_outof = dynamic_cast<const out_of_range*>(is_length);
00113                             if (is_outof) kind_of = "out_of_range";
00114                         }
00115                     }
00116                 }
00117             }
00118             else {
00119                 const runtime_error *is_runtime = dynamic_cast<const runtime_error*>(&e);
00120                 if (is_runtime) {
00121                     kind_of = "runtime_error";
00122                     const range_error *is_range = dynamic_cast<const range_error*>(is_runtime);
00123                     if (is_range) {
00124                         kind_of = "range_error";
00125                     }
00126                     else {
00127                         const overflow_error *is_overflow = dynamic_cast<const overflow_error*>(is_runtime);
00128                         if (is_overflow) {
00129                             kind_of = "overflow_error";
00130                         }
00131                         else {
00132                             const underflow_error *is_underflow = dynamic_cast<const underflow_error*>(is_runtime);
00133                             if (is_underflow) kind_of = "underflow_error";
00134                         }
00135                     }
00136                 }
00137             }
00138 
00139             throw InternalError(string("[")+kind_of+": "+e.what()+']');
00140         }
00141     };
00142 };
00143 
00144 // ---------------------------------
00145 //      Error::~Error() throw()
00146 // ---------------------------------
00147 Error::~Error() throw() {
00148 }
00149 
00150 //  ----------------------------------------------
00151 //      void Error::print(ostream& out) const
00152 //  ----------------------------------------------
00153 void Error::print(ostream& out) const {
00154     out << "\n"
00155         << get_print_message() << "\n";
00156 }
00157 
00158 //  ------------------------------------------------
00159 //      string Error::get_print_message() const
00160 //  ------------------------------------------------
00161 string Error::get_print_message() const {
00162     if (real_error) return str::strf("Error in %s: %s", error_module.c_str(), what());
00163     return str::strf("%s: %s", error_module.c_str(), what());
00164 }
00165 
00166 //  --------------------------------------------------------
00167 //      string InternalError::get_print_message() const
00168 //  --------------------------------------------------------
00169 string InternalError::get_print_message() const {
00170     return str::strf("InternalError in %s: %s\n[please report to %s]",
00171                      error_module.c_str(),
00172                      what(),
00173                      MAIL_CONTACT(module_name+".BugReport").c_str());
00174 }
00175 
00176 //  ---------------------------------------------------------------------
00177 //      string Error::set_module_name(const string& new_module_name)
00178 //  ---------------------------------------------------------------------
00179 string Error::set_module_name(const string& new_module_name) {
00180     string old_module_name = module_name;
00181     module_name = new_module_name;
00182     return old_module_name;
00183 }
00184 
00185 //  ------------------------------------------------
00186 //      IOError::IOError(const string& filename)
00187 //  ------------------------------------------------
00188 IOError::IOError(const string& filename)
00189     : Error(string(strerror(errno)) + " ('" + filename + "')")
00190 {
00191 }
00192 
00193 //  -----------------------------------------------------------------------
00194 //      IOError::IOError(const string& filename, const string& command)
00195 //  -----------------------------------------------------------------------
00196 IOError::IOError(const string& filename, const string& command)
00197     : Error(string(strerror(errno)) + " for " + command+ " ('" + filename + "')")
00198 {
00199 }
00200 
00201 

Contact me in case of errors or questions.
This documentation is powered by Doxygen.
(C) 2000-2002 Doxygen