|Constants and definitions for propagating exceptions. |
|This is the internal structure used to pass information about exceptions. More...|
|This class is used to propagate information about exceptions. More...|
Although keywords for handling exception in Objective-C don't exist on all platforms, the same effect can be achieved with classes and macros. There is a group of macros in ObjectiveLib for defining try and catch blocks, and these macros work with the OLException class to provide a nestable system of exception handlers on the call stack.
The basic usage of the macros is as follows. A try block and a catch block are both defined. If the catch block is entered, one knows that an exception was raised either within this function or futher down the call stack. Once an exception is caught, it may be referred to by the indentifier
localException. It is always of type OLException.
OL_TRY [self takeToVet: dog]; [dog setHealthy: YES]; OL_CATCH fprintf(stderr, [localException message]); OL_END_CATCH
Other macros from Exception.h provide related services. For example, if you with to return from a method from within a try block, you must use one of two macros: OL_TRY_ABANDON or OL_TRY_RETURN. If the method returns
void, you can always use OL_TRY_ABANDON, but you must otherwise use OL_TRY_RETURN. The same principle applies to the macros OL_CATCH_ABANDON and OL_CATCH_RETURN, but they are for use from within a catch block.
OL_TRY [self takeToVet: dog]; OL_TRY_RETURN(YES); OL_CATCH fprintf(stderr, [localException message]); OL_CATCH_RETURN(NO); OL_END_CATCH
There are two additional macros for use in catch blocks. In order to raise another exception from within a catch block you must use either OL_CATCH_RAISE or OL_CATCH_RAISE_STRING. The two are identical, but the second macro takes instances of OLConstantString for the first two arguments instead of C "strings".
OL_TRY [self takeToVet: dog]; OL_TRY_RETURN(YES); OL_CATCH fprintf(stderr, [localException message]); OL_CATCH_RAISE("DogException", "My dog has %s", "fleas"); OL_END_CATCH
localExceptionyou must only call its raise (OLException) method. Using OL_CATCH_RAISE to re-raise the current exception will not work.
OL_TRY [self takeToVet: dog]; OL_TRY_RETURN(YES); OL_CATCH fprintf(stderr, [localException message]); [localException raise]; OL_END_CATCH