Changeset 68 for pjproject/trunk/pjlib/include/pj/except.h
- Timestamp:
- Nov 21, 2005 4:57:02 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/include/pj/except.h
r66 r68 46 46 * 47 47 * This module provides exception handling syntactically similar to C++ in 48 * C language. The underlying mechanism use setjmp() and longjmp(), and since49 * these constructs are ANSI standard, the mechanism here should be available50 * on most platforms/compilers which are ANSI compliant.51 * 52 * If ANSI libc is not available, then setjmp()/longjmp() implementation will53 * beprovided. See <pj/compat/setjmp.h> for compatibility.48 * C language. In Win32 systems, it uses Windows Structured Exception 49 * Handling (SEH) if macro PJ_EXCEPTION_USE_WIN32_SEH is non-zero. 50 * Otherwise it will use setjmp() and longjmp(). 51 * 52 * On some platforms where setjmp/longjmp is not available, setjmp/longjmp 53 * implementation is provided. See <pj/compat/setjmp.h> for compatibility. 54 54 * 55 55 * The exception handling mechanism is completely thread safe, so the exception … … 62 62 * - You CAN NOT make nested exception in one single function without using 63 63 * a nested PJ_USE_EXCEPTION. 64 * - Exceptions will always be caught by the first handle (unlike C++ where 64 * - You can not provide more than PJ_CATCH or PJ_CATCH_ANY nor use PJ_CATCH 65 * and PJ_CATCH_ANY for a single PJ_TRY. 66 * - Exceptions will always be caught by the first handler (unlike C++ where 65 67 * exception is only caught if the type matches. 66 68 * … … 72 74 #define SYNTAX_ERROR 2 73 75 74 int main()76 int sample1() 75 77 { 76 78 PJ_USE_EXCEPTION; // declare local exception stack. … … 82 84 ... // handle exception 1 83 85 } 84 PJ_CATCH(SYNTAX_ERROR) { 85 ... // handle exception 2 86 PJ_END; 87 } 88 89 int sample2() 90 { 91 PJ_USE_EXCEPTION; // declare local exception stack. 92 93 PJ_TRY { 94 ...// do something.. 86 95 } 87 PJ_DEFAULT { 88 ... // handle other exceptions. 96 PJ_CATCH_ANY { 97 if (PJ_GET_EXCEPTION() == NO_MEMORY) 98 ...; // handle no memory situation 99 else if (PJ_GET_EXCEPTION() == SYNTAX_ERROR) 100 ...; // handle syntax error 89 101 } 90 102 PJ_END; … … 127 139 * Actually, this is just a macro to declare local variable which is used to 128 140 * push the exception state to the exception stack. 141 * Note: you must specify PJ_USE_EXCEPTION as the last statement in the 142 * local variable declarations, since it may evaluate to nothing. 129 143 * 130 144 * \subsection PJ_TRY PJ_TRY … … 138 152 * \a PJ_CATCH. 139 153 * 140 * \subsection PJ_DEFAULT PJ_DEFAULT 141 * The \a PJ_DEFAULT keyword is normally followed by a block. This block will 142 * be executed if the exception being thrown doesn't match any of the \a 143 * PJ_CATCH specification. The \a PJ_DEFAULT block \b MUST be placed as the 144 * last block of the handlers. 154 * \subsection PJ_CATCH_ANY PJ_CATCH_ANY 155 * The \a PJ_CATCH is normally followed by a block. This block will be executed 156 * if any exception was raised in the TRY block. 145 157 * 146 158 * \subsection PJ_END PJ_END … … 149 161 * \subsection PJ_GET_EXCEPTION PJ_GET_EXCEPTION(void) 150 162 * Get the last exception thrown. This macro is normally called inside the 151 * \a PJ_CATCH or \a PJ_ DEFAULTblock, altough it can be used anywhere where163 * \a PJ_CATCH or \a PJ_CATCH_ANY block, altough it can be used anywhere where 152 164 * the \a PJ_USE_EXCEPTION definition is in scope. 153 165 * … … 200 212 201 213 /** @} */ 214 215 #if defined(PJ_EXCEPTION_USE_WIN32_SEH) && PJ_EXCEPTION_USE_WIN32_SEH != 0 216 /***************************************************************************** 217 ** 218 ** IMPLEMENTATION OF EXCEPTION USING WINDOWS SEH 219 ** 220 ****************************************************************************/ 221 #define WIN32_LEAN_AND_MEAN 222 #include <windows.h> 223 224 PJ_IDECL_NO_RETURN(void) 225 pj_throw_exception_(pj_exception_id_t id) PJ_ATTR_NORETURN 226 { 227 RaiseException(id,1,0,NULL); 228 } 229 230 #define PJ_USE_EXCEPTION 231 #define PJ_TRY __try 232 #define PJ_CATCH(id) __except(GetExceptionCode()==id ? \ 233 EXCEPTION_EXECUTE_HANDLER : \ 234 EXCEPTION_CONTINUE_SEARCH) 235 #define PJ_CATCH_ANY __except(EXCEPTION_EXECUTE_HANDLER) 236 #define PJ_END 237 #define PJ_THROW(id) pj_throw_exception_(id) 238 #define PJ_GET_EXCEPTION() GetExceptionCode() 239 240 #else 241 /***************************************************************************** 242 ** 243 ** IMPLEMENTATION OF EXCEPTION USING GENERIC SETJMP/LONGJMP 244 ** 245 ****************************************************************************/ 202 246 203 247 /** … … 253 297 * @hideinitializer 254 298 */ 255 #define PJ_ DEFAULTelse299 #define PJ_CATCH_ANY else 256 300 257 301 /** … … 276 320 #define PJ_GET_EXCEPTION() (pj_x_code__) 277 321 322 #endif /* PJ_EXCEPTION_USE_WIN32_SEH */ 323 324 278 325 PJ_END_DECL 279 326
Note: See TracChangeset
for help on using the changeset viewer.