exception.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) Sergey P. Derevyago, 2008.
00003  *
00004  * Permission to copy, use, modify, sell and distribute this software is granted
00005  * provided this copyright notice appears in all copies.
00006  * This software is provided "as is" without express or implied warranty, and
00007  * with no claim as to its suitability for any purpose.
00008  *
00009  */
00010 
00017 #include <ders/exception.hpp>
00018 #include <exception>
00019 #include <ders/text_buf.hpp>
00020 
00021 namespace ders {  // ::ders
00022 
00023 sh_text toTextAll(const shException& she)
00024 {
00025  mem_pool& mp(she.pool());
00026  if (!she.get()) return nt(mp);
00027 
00028  text_buf buf(mp);
00029  for (shException e=she; ; ) {
00030      buf+e->toText();
00031 
00032      e=e->nested;
00033      if (!e.get()) break;
00034 
00035      buf+'\n';
00036  }
00037 
00038  return buf;
00039 }
00040 
00041 shException recatchException(mem_pool& mp, const FileLine& location)
00042 {
00043  try { throw; }
00044  catch (shException she) {
00045        return she;
00046  }
00047  catch (const std::exception& se) {
00048        return newStdExternException(mp, location, se.what(), typeid(se).name());
00049  }
00050  catch (...) {
00051        return newUnknExternException(mp, location, "Unknown exception");
00052  }
00053 }
00054 
00055 Exception::Exception(const FileLine& loc, const ch_rng& msg, shException nest)
00056   : location(loc), message(nt(nest.pool(), msg)), nested(nest)
00057 {
00058 }
00059 
00060 sh_text Exception::getClassName() const
00061 {
00062  return nt(message.pool(), "ders::Exception");
00063 }
00064 
00065 sh_text Exception::toText() const
00066 {
00067  return text_buf(getClassName())+" ["+location.file+":"+location.line+
00068    "], message: "+message;
00069 }
00070 
00071 shException newException(mem_pool& mp, const FileLine& loc, const ch_rng& msg)
00072 {
00073  return newException(loc, msg, shException(mp, 0));
00074 }
00075 
00076 shException newException(const FileLine& loc, const ch_rng& msg, shException
00077   nest)
00078 {
00079  mp_newbuf<Exception> buf(nest.pool());
00080  return shException(buf.pool(), buf.rls(::new(buf.get()) Exception(loc, msg,
00081    nest)));
00082 }
00083 
00084 MsgException::MsgException(const FileLine& loc, const ch_rng& msg, shException
00085   nest) : Exception(loc, msg, nest)
00086 {
00087 }
00088 
00089 ExitMsgException::ExitMsgException(const FileLine& loc, const ch_rng& msg, int
00090   ecode, shException nest) : MsgException(loc, msg, nest), exitCode(ecode)
00091 {
00092 }
00093 
00094 sh_text ExitMsgException::getClassName() const
00095 {
00096  return nt(message.pool(), "ders::ExitMsgException");
00097 }
00098 
00099 sh_text ExitMsgException::toText() const
00100 {
00101  return text_buf(getClassName())+" ["+location.file+":"+location.line+
00102    "], exitCode="+exitCode+", message: "+message;
00103 }
00104 
00105 shException newExitMsgException(mem_pool& mp, const FileLine& loc, const ch_rng&
00106   msg, int ecode)
00107 {
00108  return newExitMsgException(loc, msg, ecode, shException(mp, 0));
00109 }
00110 
00111 shException newExitMsgException(const FileLine& loc, const ch_rng& msg, int
00112   ecode, shException nest)
00113 {
00114  mp_newbuf<ExitMsgException> buf(nest.pool());
00115  return shException(buf.pool(), buf.rls(::new(buf.get()) ExitMsgException(loc,
00116    msg, ecode, nest)));
00117 }
00118 
00119 ExternException::ExternException(const FileLine& loc, const ch_rng& msg, const
00120   ch_rng& tname, shException nest) :
00121   Exception(loc, msg, nest), typeName(nt(nest.pool(), tname))
00122 {
00123 }
00124 
00125 sh_text ExternException::toText() const
00126 {
00127  return text_buf(getClassName())+" ["+location.file+":"+location.line+
00128    "], typeName=\""+typeName+"\", message: "+message;
00129 }
00130 
00131 StdExternException::StdExternException(const FileLine& loc, const ch_rng& msg,
00132   const ch_rng& tname, shException nest) :
00133   ExternException(loc, msg, tname, nest)
00134 {
00135 }
00136 
00137 sh_text StdExternException::getClassName() const
00138 {
00139  return nt(message.pool(), "ders::StdExternException");
00140 }
00141 
00142 shException newStdExternException(mem_pool& mp, const FileLine& loc, const
00143   ch_rng& msg, const ch_rng& tname)
00144 {
00145  return newStdExternException(loc, msg, tname, shException(mp, 0));
00146 }
00147 
00148 shException newStdExternException(const FileLine& loc, const ch_rng& msg, const
00149   ch_rng& tname, shException nest)
00150 {
00151  mp_newbuf<StdExternException> buf(nest.pool());
00152  return shException(buf.pool(), buf.rls(::new(buf.get()) StdExternException(loc,
00153    msg, tname, nest)));
00154 }
00155 
00156 UnknExternException::UnknExternException(const FileLine& loc, const ch_rng& msg,
00157   shException nest) :
00158   ExternException(loc, msg, nt(nest.pool(), "unknown"), nest)
00159 {
00160 }
00161 
00162 sh_text UnknExternException::getClassName() const
00163 {
00164  return nt(message.pool(), "ders::UnknExternException");
00165 }
00166 
00167 shException newUnknExternException(mem_pool& mp, const FileLine& loc, const
00168   ch_rng& msg)
00169 {
00170  return newUnknExternException(loc, msg, shException(mp, 0));
00171 }
00172 
00173 shException newUnknExternException(const FileLine& loc, const ch_rng& msg,
00174   shException nest)
00175 {
00176  mp_newbuf<UnknExternException> buf(nest.pool());
00177  return shException(buf.pool(), buf.rls(::new(buf.get()) UnknExternException(
00178    loc, msg, nest)));
00179 }
00180 
00181 }  // namespace ::ders
00182 

Generated on Mon Jul 28 18:39:55 2008 for derslib by  doxygen 1.5.5