exception.cpp

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

Generated on Tue Dec 8 11:35:32 2009 for derslib by  doxygen 1.5.5