00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <execinfo.h>
00027
00028 #include <stlib/GenericException.h>
00029 #include <stlib/Array.h>
00030 #include <stlib/String.h>
00031 #include <stlib/Stream.h>
00032 #include <stlib/Visitor.h>
00033
00034
00035
00036 GenericException *GenericException::last_exception = nil;
00037
00038 GenericException::GenericException(String *message, String *selector,
00039 Object *param)
00040 {
00041 messageText = message;
00042 _selector = selector;
00043 _parameter = param;
00044 _originator = nil;
00045 stack_size = 0;
00046 }
00047
00048 GenericException::~GenericException(void)
00049 {
00050 }
00051
00052
00053 String *GenericException::className(void) const
00054 {
00055 return new String("GenericException");
00056 }
00057
00058 GenericException *GenericException::lastException(void)
00059 {
00060 return last_exception;
00061 }
00062
00063
00064 String *GenericException::description(void)
00065 {
00066 if (messageText == NULL)
00067 return notifierString();
00068 return messageText;
00069 }
00070
00071 const Object *GenericException::originator(void)
00072 {
00073 if (_originator == nil) return this;
00074 else return _originator;
00075 }
00076
00077 void GenericException::originator(const Object *object)
00078 {
00079 _originator = object;
00080 }
00081
00082 Object *GenericException::parameter(void)
00083 {
00084 return _parameter;
00085 }
00086
00087 void GenericException::selector(String *sel)
00088 {
00089 _selector = sel;
00090 }
00091
00092 void GenericException::selector(const char *sel)
00093 {
00094 _selector = new String(sel);
00095 }
00096
00097 String *GenericException::selector(void)
00098 {
00099 return _selector;
00100 }
00101
00102
00103 void GenericException::printStackOn(Stream *stream)
00104 {
00105 if (method_stack == NULL || stack_size <= 1) {
00106 stream->nextPutAll("No stack.");
00107 return;
00108 }
00109
00110 char **names;
00111 String *line, *module, *mangledName;
00112 int startMark, endMark;
00113
00114 names = backtrace_symbols(method_stack, stack_size);
00115 for (int i = 1; i < stack_size; i++) {
00116 line = new String(names[i]);
00117 startMark = line->indexOf('(');
00118 if (startMark < 0) startMark = line->indexOf(' ');
00119 endMark = line->nextIndexOf('+', startMark, line->size());
00120 module = dynamic_cast<String *>(line->copy(0, startMark));
00121 mangledName = dynamic_cast<String *>(line->copy(startMark+1, endMark));
00122
00123 stream->nextPutAll(" ");
00124 try {
00125 stream->nextPutAll(mangledName->demangleMethodName());
00126 }
00127 catch (GenericException *ex) {
00128 stream->nextPutAll(mangledName);
00129 }
00130 stream->nextPutAll(" in ");
00131 stream->nextPutAll(module);
00132 stream->lf();
00133 }
00134 free((void *) names);
00135 }
00136
00137
00138 void GenericException::pass(void)
00139 {
00140 privRaise();
00141 }
00142
00143 void GenericException::raiseFrom(const Object *origin)
00144 {
00145 originator(origin);
00146 raise();
00147 }
00148
00149 void GenericException::raise(void)
00150 {
00151 getExecutionStack();
00152 privRaise();
00153 }
00154
00155
00156 void GenericException::visitBy(Visitor *visitor)
00157 {
00158 visitor->visitException(this);
00159 }
00160
00161
00162 void GenericException::getExecutionStack(void)
00163 {
00164
00165
00166 last_exception = this;
00167 stack_size = backtrace(method_stack, MAXSTACKSIZE);
00168 }