00001 /* 00002 * GenericException.h 00003 * 00004 * Smalltalk like class library for C++ 00005 * Abstract exception. Header. 00006 * 00007 * Copyright (c) 2003 Milan Cermak 00008 */ 00009 /* 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 */ 00024 #ifndef __GENERICEXCEPTION_H 00025 #define __GENERICEXCEPTION_H 00026 00027 #include <stlib/Object.h> 00028 00029 #define MAXSTACKSIZE 64 00030 00031 namespace Core { 00032 00033 class String; 00034 class Stream; 00035 00039 class GenericException : public Object 00040 { 00041 protected: 00042 String *messageText; //< Exception description 00043 String *_selector; //< Method the exception occured in 00044 const Object *_originator; //< Object the exception occured in 00045 Object *_parameter; //< Argument that possibly caused the exception 00046 00047 static GenericException *last_exception; //< What was the last raised exception 00048 00049 private: 00050 /* These variables are too low level. Better not to play with them. */ 00051 void *method_stack[MAXSTACKSIZE]; //< Array of methods that was on stack when an exception occurs 00052 int stack_size; //< Size of method_stack 00053 00054 public: 00055 GenericException(String *message = nil, String *selector = nil, 00056 Object *param = nil); 00057 00058 virtual ~GenericException(void); 00059 00060 /* Class-accessing protocol */ 00061 virtual String *className(void) const; 00062 static GenericException *lastException(void); 00063 00064 /* Accessing protocol */ 00065 virtual String *description(void); 00066 virtual String *notifierString(void) abstract; 00067 virtual const Object *originator(void); 00068 virtual void originator(const Object *object); 00069 virtual Object *parameter(void); 00070 virtual void selector(String *sel); 00071 virtual void selector(const char *sel); 00072 virtual String *selector(void); 00073 00074 /* Printing protocol */ 00075 virtual void printStackOn(Stream *stream); 00076 00077 /* Raising protocol */ 00081 virtual void pass(void); 00082 00083 virtual void raiseFrom(const Object *origin); 00084 00088 virtual void raise(void); 00089 00090 /* Visiting protocol */ 00091 virtual void visitBy(Visitor *visitor); 00092 00093 protected: 00094 /* Private protocol */ 00095 virtual void getExecutionStack(void); 00100 virtual void privRaise(void) abstract; 00101 }; 00102 00119 #define _ensure(guardedBlock, ensureBlock) \ 00120 GenericException *_catched_exception = nil; \ 00121 try { guardedBlock ; } \ 00122 catch (GenericException *ex) { _catched_exception = ex; } \ 00123 ensureBlock ; \ 00124 if (_catched_exception != nil) _catched_exception->pass() 00125 00126 }; 00127 00128 #endif /* __GENERICEXCEPTION_H */