00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZEExceptioncpp)
00056 #define ALIZEExceptioncpp
00057
00058 #include "Exception.h"
00059
00060
00061 #include <cstdio>
00062
00063 #if defined(__GNUC__)
00064 #include <unistd.h>
00065 #endif
00066
00067
00068
00069 using namespace alize;
00070
00071
00072 Exception::Exception(const String& msg, const String& sourceFile, int line)
00073 :Object(), msg(msg), sourceFile(sourceFile), line(line), trace(stackTrace(getClassName())) {}
00074
00075
00076
00077
00078
00079 Exception::Exception(const String& msg, const String& sourceFile, int line, String callerName)
00080 :Object(), msg(msg), sourceFile(sourceFile), line(line), trace(stackTrace(callerName)) {}
00081
00082 Exception::Exception(const Exception& e)
00083 :Object(),msg(e.msg),sourceFile(e.sourceFile),line(e.line),trace(e.trace) {}
00084
00085 String Exception::toString() const
00086 {
00087 return trace
00088 + Object::toString()
00089 + "\n message = \"" + msg + "\""
00090 + "\n source file = " + sourceFile
00091 + "\n line number = " + String::valueOf(line);
00092 }
00093
00094 String Exception::getClassName() const { return "Exception"; }
00095
00122 String Exception::stackTrace( const String callerName) const
00123 {
00124
00125
00128 if( callerName == "EOFException") {
00129 return "" ;
00130 }
00131 if( callerName == "OutOfMemoryException") {
00132
00133 fprintf(stderr, "OutOfMemoryException: Exception::stackTrace() won't succeed in tracing the stack...");
00134 return "OutOfMemoryException: Exception::stackTrace() won't succeed in tracing the stack..." ;
00135 }
00136
00140
00141
00142
00143
00144 String data ;
00145
00146 #if defined(linux) || defined(__linux) ///< fetch required bits (workaround the fact we don't have access to any info (argv[0] i.e.) )
00147 char* mistralProg = new char[2048];
00148 size_t length = readlink("/proc/self/exe", mistralProg, 2048);
00149 mistralProg[length] = '\0';
00150 char* myPid = new char[9];
00151 sprintf( myPid, "%d", getpid() ) ;
00152
00153
00154
00155
00156 #if defined(THREAD) //note: no threads in ALIZE!
00157 String cmd = "gdb --batch -ex='info threads' -ex=bt " + String(mistralProg) +" "+ String(myPid) ;
00158 #else
00159 String cmd = "gdb --batch -ex=bt " + String(mistralProg) +" "+ String(myPid) ;
00160 #endif
00161 data += callerName + "; stack trace by GDB [" + cmd +"]\n" ;
00162
00163
00164 size_t MAX_BUFFER = 255 ;
00165 FILE *stream;
00166 char buffer[MAX_BUFFER];
00167 stream = popen( cmd.c_str(), "r");
00168 while( fgets(buffer, MAX_BUFFER, stream) != NULL ) {
00169 data += buffer ;
00170 }
00171 pclose(stream);
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 #else
00189 data = " *** Exception::stackTrace() uses gdb and GNU/Linux' /proc fs which are not available on others systems - won't trace the stack.\n" ;
00190 #endif
00191
00192
00193
00194 return data;
00195 }
00196
00197 Exception::~Exception() {}
00198
00199
00200
00201
00202
00203 IndexOutOfBoundsException::IndexOutOfBoundsException(const String& msg,
00204 const String& sourceFile, int line, long index, long limit)
00205 :Exception(msg, sourceFile, line, getClassName()), index(index), limit(limit) {}
00206
00207 IndexOutOfBoundsException::IndexOutOfBoundsException(
00208 const IndexOutOfBoundsException& e)
00209 :Exception(e.msg, e.sourceFile, e.line, getClassName()),
00210 index(e.index), limit(e.limit) {}
00211
00212 String IndexOutOfBoundsException::toString() const
00213 {
00214 return Exception::toString()
00215 + "\n index " + String::valueOf(index)
00216 + " >= limit " + String::valueOf(limit);
00217 }
00218
00219 String IndexOutOfBoundsException::getClassName() const
00220 { return "IndexOutOfBoundsException"; }
00221
00222 IndexOutOfBoundsException::~IndexOutOfBoundsException() {}
00223
00224
00225
00226
00227
00228
00229 IOException::IOException(const String& msg, const String& sourceFile,
00230 int line, const FileName& f)
00231 :Exception(msg, sourceFile, line, getClassName()), fileName(f) {}
00232
00233 IOException::IOException(const String& msg, const String& sourceFile,
00234 int line, const FileName& f, const String callerName)
00235 :Exception(msg, sourceFile, line, callerName), fileName(f) {}
00236
00237 IOException::IOException(const IOException& e)
00238 :Exception(e.msg, e.sourceFile, e.line, getClassName()) {}
00239
00240 String IOException::toString() const
00241 { return Exception::toString() + "\n fileName = " + fileName; }
00242
00243 String IOException::getClassName() const { return "IOException"; }
00244
00245 IOException::~IOException() {}
00246
00247
00248
00249
00250
00251
00252 IdAlreadyExistsException::IdAlreadyExistsException(const String& msg,
00253 const String& sourceFile, int line)
00254 :Exception(msg, sourceFile, line, getClassName()) {}
00255
00256 IdAlreadyExistsException::IdAlreadyExistsException(
00257 const IdAlreadyExistsException& e)
00258 :Exception(e.msg, e.sourceFile, e.line, getClassName()) {}
00259
00260 String IdAlreadyExistsException::getClassName() const
00261 { return "IdAlreadyExistsException"; }
00262
00263 IdAlreadyExistsException::~IdAlreadyExistsException() {}
00264
00265
00266
00267
00268
00269
00270 InvalidDataException::InvalidDataException(const String& msg,
00271 const String& sourceFile, int line, const FileName& f)
00272 :IOException(msg, sourceFile, line, f, getClassName()) {}
00273
00274 InvalidDataException::InvalidDataException(const InvalidDataException& e)
00275 :IOException(e.msg, e.sourceFile, e.line, e.fileName, getClassName()) {}
00276
00277 String InvalidDataException::getClassName() const
00278 { return "InvalidDataException"; }
00279
00280 InvalidDataException::~InvalidDataException() {}
00281
00282
00283
00284
00285
00286
00287 OutOfMemoryException::OutOfMemoryException(const String& msg,
00288 const String& sourceFile, int line)
00289 :Exception(msg, sourceFile, line, getClassName()) {}
00290
00291 OutOfMemoryException::OutOfMemoryException(const OutOfMemoryException& e)
00292 :Exception(e.msg, e.sourceFile, e.line, getClassName()) {}
00293
00294 String OutOfMemoryException::getClassName() const
00295 { return "OutOfMemoryException"; }
00296
00297 OutOfMemoryException::~OutOfMemoryException() {}
00298
00299
00300
00301
00302
00303
00304 FileNotFoundException::FileNotFoundException(const String& msg,
00305 const String& sourceFile, int line, const FileName& f)
00306 :IOException(msg, sourceFile, line, f, getClassName()) {}
00307
00308 FileNotFoundException::FileNotFoundException(
00309 const FileNotFoundException& e)
00310 :IOException(e.msg, e.sourceFile, e.line, e.fileName, getClassName()) {}
00311
00312 String FileNotFoundException::getClassName() const
00313 { return "FileNotFoundException"; }
00314
00315 FileNotFoundException::~FileNotFoundException() {}
00316
00317
00318
00319
00320
00321
00322 ParamNotFoundInConfigException::ParamNotFoundInConfigException(const String& msg,
00323 const String& sourceFile, int line)
00324 :Exception(msg, sourceFile, line, getClassName()) {}
00325
00326 ParamNotFoundInConfigException::ParamNotFoundInConfigException(
00327 const ParamNotFoundInConfigException& e)
00328 :Exception(e.msg, e.sourceFile, e.line, getClassName()) {}
00329
00330 String ParamNotFoundInConfigException::getClassName() const
00331 { return "ParamNotFoundInConfigException"; }
00332
00333 ParamNotFoundInConfigException::~ParamNotFoundInConfigException() {}
00334
00335
00336
00337
00338
00339
00340 ConfigCheckException::ConfigCheckException(const String& msg,
00341 const String& sourceFile, int line)
00342 :Exception(msg, sourceFile, line, getClassName()) {}
00343
00344 ConfigCheckException::ConfigCheckException(
00345 const ConfigCheckException& e)
00346 :Exception(e.msg, e.sourceFile, e.line, getClassName()) {}
00347
00348 String ConfigCheckException::getClassName() const
00349 { return "ConfigCheckException"; }
00350
00351 ConfigCheckException::~ConfigCheckException() {}
00352
00353
00354
00355
00356
00357
00358 EOFException::EOFException(const String& msg, const String& sourceFile,
00359 int line, const FileName& f)
00360 :IOException(msg, sourceFile, line, f, getClassName()) {}
00361
00362 EOFException::EOFException(const EOFException& e)
00363 :IOException(e.msg, e.sourceFile, e.line, e.fileName, getClassName()) {}
00364
00365 String EOFException::getClassName() const { return "EOFException"; }
00366
00367 EOFException::~EOFException() {}
00368
00369
00370
00371
00372
00373
00374
00375
00376 #endif // !defined(ALIZEExceptioncpp)