gcc: Exceptions
1
1 8.7 Exceptions
1 ==============
1
1 GNU Objective-C provides exception support built into the language, as
1 in the following example:
1
1 @try {
1 ...
1 @throw expr;
1 ...
1 }
1 @catch (AnObjCClass *exc) {
1 ...
1 @throw expr;
1 ...
1 @throw;
1 ...
1 }
1 @catch (AnotherClass *exc) {
1 ...
1 }
1 @catch (id allOthers) {
1 ...
1 }
1 @finally {
1 ...
1 @throw expr;
1 ...
1 }
1
1 The '@throw' statement may appear anywhere in an Objective-C or
1 Objective-C++ program; when used inside of a '@catch' block, the
1 '@throw' may appear without an argument (as shown above), in which case
1 the object caught by the '@catch' will be rethrown.
1
1 Note that only (pointers to) Objective-C objects may be thrown and
1 caught using this scheme. When an object is thrown, it will be caught
1 by the nearest '@catch' clause capable of handling objects of that type,
1 analogously to how 'catch' blocks work in C++ and Java. A '@catch(id
1 ...)' clause (as shown above) may also be provided to catch any and all
1 Objective-C exceptions not caught by previous '@catch' clauses (if any).
1
1 The '@finally' clause, if present, will be executed upon exit from the
1 immediately preceding '@try ... @catch' section. This will happen
1 regardless of whether any exceptions are thrown, caught or rethrown
1 inside the '@try ... @catch' section, analogously to the behavior of the
1 'finally' clause in Java.
1
1 There are several caveats to using the new exception mechanism:
1
1 * The '-fobjc-exceptions' command line option must be used when
1 compiling Objective-C files that use exceptions.
1
1 * With the GNU runtime, exceptions are always implemented as "native"
1 exceptions and it is recommended that the '-fexceptions' and
1 '-shared-libgcc' options are used when linking.
1
1 * With the NeXT runtime, although currently designed to be binary
1 compatible with 'NS_HANDLER'-style idioms provided by the
1 'NSException' class, the new exceptions can only be used on Mac OS
1 X 10.3 (Panther) and later systems, due to additional functionality
1 needed in the NeXT Objective-C runtime.
1
1 * As mentioned above, the new exceptions do not support handling
1 types other than Objective-C objects. Furthermore, when used from
1 Objective-C++, the Objective-C exception model does not
1 interoperate with C++ exceptions at this time. This means you
1 cannot '@throw' an exception from Objective-C and 'catch' it in
1 C++, or vice versa (i.e., 'throw ... @catch').
1