Detailed DescriptionQuick ExampleFor the impatient, take a look at some examples: Exception HandlingThis module provides exception handling syntactically similar to C++ in C language. In Win32 systems, it uses Windows Structured Exception Handling (SEH) if macro PJ_EXCEPTION_USE_WIN32_SEH is non-zero. Otherwise it will use setjmp() and longjmp(). On some platforms where setjmp/longjmp is not available, setjmp/longjmp implementation is provided. See <pj/compat/setjmp.h> for compatibility. The exception handling mechanism is completely thread safe, so the exception thrown by one thread will not interfere with other thread. The exception handling constructs are similar to C++. The blocks will be constructed similar to the following sample:
#define NO_MEMORY 1
#define SYNTAX_ERROR 2
int sample1()
{
PJ_USE_EXCEPTION; // declare local exception stack.
PJ_TRY {
...// do something..
}
PJ_CATCH(NO_MEMORY) {
... // handle exception 1
}
PJ_END;
}
int sample2()
{
PJ_USE_EXCEPTION; // declare local exception stack.
PJ_TRY {
...// do something..
}
PJ_CATCH_ANY {
if (PJ_GET_EXCEPTION() == NO_MEMORY)
...; // handle no memory situation
else if (PJ_GET_EXCEPTION() == SYNTAX_ERROR)
...; // handle syntax error
}
PJ_END;
}
The above sample uses hard coded exception ID. It is strongly recommended that applications request a unique exception ID instead of hard coded value like above. Exception ID AllocationTo ensure that exception ID (number) are used consistently and to prevent ID collisions in an application, it is strongly suggested that applications allocate an exception ID for each possible exception type. As a bonus of this process, the application can identify the name of the exception when the particular exception is thrown. Exception ID management are performed with the following APIs: PJLIB itself automatically allocates one exception id, i.e. PJ_NO_MEMORY_EXCEPTION which is declared in <pj/pool.h>. This exception ID is raised by default pool policy when it fails to allocate memory. CAVEATS:
KeywordsPJ_THROW(expression)Throw an exception. The expression thrown is an integer as the result of the expression. This keyword can be specified anywhere within the program. PJ_USE_EXCEPTIONSpecify this in the variable definition section of the function block (or any blocks) to specify that the block has PJ_TRY/PJ_CATCH exception block. Actually, this is just a macro to declare local variable which is used to push the exception state to the exception stack. Note: you must specify PJ_USE_EXCEPTION as the last statement in the local variable declarations, since it may evaluate to nothing. PJ_TRYThe PJ_TRY keyword is typically followed by a block. If an exception is thrown in this block, then the execution will resume to the PJ_CATCH handler. PJ_CATCH(expression)The PJ_CATCH is normally followed by a block. This block will be executed if the exception being thrown is equal to the expression specified in the PJ_CATCH. PJ_CATCH_ANYThe PJ_CATCH is normally followed by a block. This block will be executed if any exception was raised in the TRY block. PJ_ENDSpecify this keyword to mark the end of PJ_TRY / PJ_CATCH blocks. PJ_GET_EXCEPTION(void)Get the last exception thrown. This macro is normally called inside the PJ_CATCH or PJ_CATCH_ANY block, altough it can be used anywhere where the PJ_USE_EXCEPTION definition is in scope. ExamplesFor some examples on how to use the exception construct, please see: Function Documentation
Allocate a unique exception id. Applications don't have to allocate a unique exception ID before using the exception construct. However, by doing so it ensures that there is no collisions of exception ID. As a bonus, when exception number is acquired through this function, the library can assign name to the exception (only if PJ_HAS_EXCEPTION_NAMES is enabled (default is yes)) and find out the exception name when it catches an exception.
Free an exception id.
Retrieve name associated with the exception id.
Copyright (C) 2006-2009 Teluu Inc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||