JWS C++ Library
C++ language utility library
exception.cpp
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00046 #include <jwsc++/config.h>
00047 
00048 #include <string>
00049 #include <iostream>
00050 
00051 #include <inttypes.h>
00052 
00053 #include "jwsc++/base/exception.h"
00054 
00055 #ifdef JWSCXX_HAVE_DMALLOC
00056 #include <dmalloc.h>
00057 #endif
00058 
00059 
00060 using std::string;
00061 using std::ostream;
00062 using std::cerr;
00063 using std::fprintf;
00064 using std::exit;
00065 using std::abort;
00066 
00067 using namespace jwscxx::base;
00068 
00069 
00070 /* --------------------------  Exception  ----------------------------------- */
00071 
00077 Exception::Exception(const char* msg, const char* file, uint32_t line)
00078     : msg(msg)
00079 {
00080     this->file = file;
00081     this->line = line;
00082 }
00083 
00084 
00090 Exception::Exception(const string& msg, const char* file, uint32_t line)
00091     : msg(msg)
00092 {
00093     this->file = file;
00094     this->line = line;
00095 }
00096 
00097 
00099 Exception::Exception(const Exception& e)
00100     : msg(e.msg)
00101 {
00102     file = e.file;
00103     line = e.line;
00104 }
00105 
00106 
00111 void Exception::print(ostream& out, bool newline) const
00112 {
00113     out << msg;
00114 
00115     if (newline) out << "\n";
00116 }
00117 
00118 
00123 void Exception::print_details(ostream& out, bool newline) const
00124 {
00125     if (file && line)
00126     {
00127         out << file << " line " << line << ": " << msg;
00128         if (newline) out << "\n";
00129     }
00130     else if (file)
00131     {
00132         out << file << ": " << msg;
00133         if (newline) out << "\n";
00134     }
00135     else
00136     {
00137         print(out, newline);
00138     }
00139 }
00140 
00141 
00146 void Exception::print_abort(ostream& out, bool newline) const
00147 {
00148     print(out, newline);
00149     abort();
00150 }
00151 
00152 
00157 void Exception::print_details_abort(ostream& out, bool newline) const
00158 {
00159     print_details(out, newline);
00160     abort();
00161 }
00162 
00163 
00169 void Exception::print_exit(ostream& out, bool newline, int status) const
00170 {
00171     print(out, newline);
00172     exit(status);
00173 }
00174 
00175 
00181 void Exception::print_details_exit(ostream& out, bool newline, int status) const
00182 {
00183     print_details(out, newline);
00184     exit(status);
00185 }
00186 
00187 /* -------------------------------------------------------------------------- */
00188 
00189 
00190 
00191 
00192 
00193 
00194 
00195 /* --------------------------  Exception  ----------------------------------- */
00196 
00202 IO_error::IO_error(const char* msg, const char* file, uint32_t line)
00203     : Exception(msg, file, line)
00204 {
00205 }
00206 
00207 
00213 IO_error::IO_error(const string& msg, const char* file, uint32_t line)
00214     : Exception(msg, file, line)
00215 {
00216 }
00217 
00218 
00220 IO_error::IO_error(const IO_error& e)
00221     : Exception(e)
00222 {
00223 }
00224 
00225 /* -------------------------------------------------------------------------- */
00226 
00227 
00228 
00229 
00230 
00231 
00232 
00233 /* --------------------------  Exception  ----------------------------------- */
00234 
00240 Arg_error::Arg_error(const char* msg, const char* file, uint32_t line)
00241     : Exception(msg, file, line)
00242 {
00243 }
00244 
00245 
00251 Arg_error::Arg_error(const string& msg, const char* file, uint32_t line)
00252     : Exception(msg, file, line)
00253 {
00254 }
00255 
00256 
00258 Arg_error::Arg_error(const Arg_error& e)
00259     : Exception(e)
00260 {
00261 }
00262 
00263 /* -------------------------------------------------------------------------- */
00264 
00265 
00266 
00267 
00268 
00269 
00270 /* --------------------------  Exception  ----------------------------------- */
00271 
00277 Result_error::Result_error(const char* msg, const char* file, uint32_t line)
00278     : Exception(msg, file, line)
00279 {
00280 }
00281 
00282 
00288 Result_error::Result_error(const string& msg, const char* file, uint32_t line)
00289     : Exception(msg, file, line)
00290 {
00291 }
00292 
00293 
00295 Result_error::Result_error(const Result_error& e)
00296     : Exception(e)
00297 {
00298 }
00299 
00300 /* -------------------------------------------------------------------------- */