1<html lang="en"> 2<head> 3<title>Exceptions - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Objective_002dC.html#Objective_002dC" title="Objective-C"> 9<link rel="prev" href="compatibility_005falias.html#compatibility_005falias" title="compatibility_alias"> 10<link rel="next" href="Synchronization.html#Synchronization" title="Synchronization"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Exceptions"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Synchronization.html#Synchronization">Synchronization</a>, 53Previous: <a rel="previous" accesskey="p" href="compatibility_005falias.html#compatibility_005falias">compatibility_alias</a>, 54Up: <a rel="up" accesskey="u" href="Objective_002dC.html#Objective_002dC">Objective-C</a> 55<hr> 56</div> 57 58<h3 class="section">8.7 Exceptions</h3> 59 60<p>GNU Objective-C provides exception support built into the language, as 61in the following example: 62 63<pre class="smallexample"> @try { 64 ... 65 @throw expr; 66 ... 67 } 68 @catch (AnObjCClass *exc) { 69 ... 70 @throw expr; 71 ... 72 @throw; 73 ... 74 } 75 @catch (AnotherClass *exc) { 76 ... 77 } 78 @catch (id allOthers) { 79 ... 80 } 81 @finally { 82 ... 83 @throw expr; 84 ... 85 } 86</pre> 87 <p>The <code>@throw</code> statement may appear anywhere in an Objective-C or 88Objective-C++ program; when used inside of a <code>@catch</code> block, the 89<code>@throw</code> may appear without an argument (as shown above), in 90which case the object caught by the <code>@catch</code> will be rethrown. 91 92 <p>Note that only (pointers to) Objective-C objects may be thrown and 93caught using this scheme. When an object is thrown, it will be caught 94by the nearest <code>@catch</code> clause capable of handling objects of 95that type, analogously to how <code>catch</code> blocks work in C++ and 96Java. A <code>@catch(id ...)</code> clause (as shown above) may also 97be provided to catch any and all Objective-C exceptions not caught by 98previous <code>@catch</code> clauses (if any). 99 100 <p>The <code>@finally</code> clause, if present, will be executed upon exit 101from the immediately preceding <code>@try ... @catch</code> section. 102This will happen regardless of whether any exceptions are thrown, 103caught or rethrown inside the <code>@try ... @catch</code> section, 104analogously to the behavior of the <code>finally</code> clause in Java. 105 106 <p>There are several caveats to using the new exception mechanism: 107 108 <ul> 109<li>The <samp><span class="option">-fobjc-exceptions</span></samp> command line option must be used when 110compiling Objective-C files that use exceptions. 111 112 <li>With the GNU runtime, exceptions are always implemented as “native” 113exceptions and it is recommended that the <samp><span class="option">-fexceptions</span></samp> and 114<samp><span class="option">-shared-libgcc</span></samp> options are used when linking. 115 116 <li>With the NeXT runtime, although currently designed to be binary 117compatible with <code>NS_HANDLER</code>-style idioms provided by the 118<code>NSException</code> class, the new exceptions can only be used on Mac 119OS X 10.3 (Panther) and later systems, due to additional functionality 120needed in the NeXT Objective-C runtime. 121 122 <li>As mentioned above, the new exceptions do not support handling 123types other than Objective-C objects. Furthermore, when used from 124Objective-C++, the Objective-C exception model does not interoperate with C++ 125exceptions at this time. This means you cannot <code>@throw</code> an exception 126from Objective-C and <code>catch</code> it in C++, or vice versa 127(i.e., <code>throw ... @catch</code>). 128</ul> 129 130<!-- ========================================================================= --> 131 </body></html> 132 133