00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00015 #include "cexception.hpp"
00016 #include <exception>
00017 #include <typeinfo>
00018 #include "stringbuf.hpp"
00019
00020 using namespace std;
00021
00022 CException::CException(const FileLine& loc, const std::string& msg,
00023 sh_ptr<CException> nest) : location(loc), message(msg), nested(nest)
00024 {
00025 }
00026
00027 CException::~CException()
00028 {
00029 }
00030
00031 std::string CException::getClassName() const
00032 {
00033 return "CException";
00034 }
00035
00036 std::string CException::toString() const
00037 {
00038 return StringBuf()+getClassName()+" ["+location.fname+":"+location.line+"]: "+
00039 message;
00040 }
00041
00042 std::string CException::toStringAll() const
00043 {
00044 string ret;
00045 ret.reserve(1024);
00046
00047 for (const CException* ptr=this; ptr; ptr=ptr->nested.get()) {
00048 ret.append(ptr->toString());
00049 ret.push_back('\n');
00050 }
00051
00052 return ret;
00053 }
00054
00055 ExternalCException::ExternalCException(const FileLine& loc,
00056 const std::string& msg, const std::string& tname, sh_ptr<CException> nest) :
00057 CException(loc, msg, nest), typeName(tname)
00058 {
00059 }
00060
00061 ExternalCException::~ExternalCException()
00062 {
00063 }
00064
00065 std::string ExternalCException::getClassName() const
00066 {
00067 return "ExternalCException";
00068 }
00069
00070 std::string ExternalCException::toString() const
00071 {
00072 return StringBuf()+getClassName()+" ["+location.fname+":"+location.line+
00073 "], typeName=\""+typeName+"\", message=\""+message+"\"";
00074 }
00075
00076 STDExternalCException::STDExternalCException(const FileLine& loc,
00077 const std::string& msg, const std::string& tname, sh_ptr<CException> nest) :
00078 ExternalCException(loc, msg, tname, nest)
00079 {
00080 }
00081
00082 STDExternalCException::~STDExternalCException()
00083 {
00084 }
00085
00086 std::string STDExternalCException::getClassName() const
00087 {
00088 return "STDExternalCException";
00089 }
00090
00091 UnknownExternalCException::UnknownExternalCException(const FileLine& loc,
00092 const std::string& msg, const std::string& tname, sh_ptr<CException> nest) :
00093 ExternalCException(loc, msg, tname, nest)
00094 {
00095 }
00096
00097 UnknownExternalCException::~UnknownExternalCException()
00098 {
00099 }
00100
00101 std::string UnknownExternalCException::getClassName() const
00102 {
00103 return "UnknownExternalCException";
00104 }
00105
00106 sh_ptr<CException> toCException(const CException::FileLine& location)
00107 {
00108 #ifndef DERS_RETHROW_BUG // нормальная версия
00109 try { throw; }
00110 catch (sh_ptr<CException> ce) {
00111 return ce;
00112 }
00113 catch (const exception& e) {
00114 return newSTDExternalCException(location, e.what(), typeid(e).name());
00115 }
00116 catch (...) {
00117 return newUnknownExternalCException(location, "Unknown exception",
00118 "unknown");
00119 }
00120 #else // версия для компиляторов с ошибками
00121 if (CException::current.refs()>1 && CException::current.get())
00122 return CException::current;
00123
00124 return newUnknownExternalCException(location, "Unknown exception", "unknown");
00125 #endif
00126 }
00127
00128 #ifdef DERS_RETHROW_BUG
00129 sh_ptr<CException> CException::current;
00130
00131 sh_ptr<CException> newCException(const CException::FileLine& loc,
00132 const std::string& msg, sh_ptr<CException> nest)
00133 {
00134 return CException::current=sh_ptr<CException>(new CException(loc, msg, nest));
00135 }
00136
00137 sh_ptr<CException> newExternalCException(const CException::FileLine& loc,
00138 const std::string& msg, const std::string& tname, sh_ptr<CException> nest)
00139 {
00140 return CException::current=sh_ptr<CException>(new ExternalCException(loc, msg,
00141 tname, nest));
00142 }
00143
00144 sh_ptr<CException> newSTDExternalCException(
00145 const CException::FileLine& loc, const std::string& msg,
00146 const std::string& tname, sh_ptr<CException> nest)
00147 {
00148 return CException::current=sh_ptr<CException>(new STDExternalCException(loc,
00149 msg, tname, nest));
00150 }
00151
00152 sh_ptr<CException> newUnknownExternalCException(
00153 const CException::FileLine& loc, const std::string& msg,
00154 const std::string& tname, sh_ptr<CException> nest)
00155 {
00156 return CException::current=sh_ptr<CException>(new UnknownExternalCException(
00157 loc, msg, tname, nest));
00158 }
00159 #endif