Exceptions.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Exceptions</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Exceptions">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Exceptions">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Objective_002dC.html#Objective_002dC" rel="up" title="Objective-C">
  30. <link href="Synchronization.html#Synchronization" rel="next" title="Synchronization">
  31. <link href="compatibility_005falias.html#compatibility_005falias" rel="prev" title="compatibility_alias">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Exceptions"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Synchronization.html#Synchronization" accesskey="n" rel="next">Synchronization</a>, Previous: <a href="compatibility_005falias.html#compatibility_005falias" accesskey="p" rel="prev">compatibility_alias</a>, Up: <a href="Objective_002dC.html#Objective_002dC" accesskey="u" rel="up">Objective-C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Exceptions-1"></a>
  69. <h3 class="section">8.7 Exceptions</h3>
  70. <p>GNU Objective-C provides exception support built into the language, as
  71. in the following example:
  72. </p>
  73. <div class="smallexample">
  74. <pre class="smallexample"> @try {
  75. &hellip;
  76. @throw expr;
  77. &hellip;
  78. }
  79. @catch (AnObjCClass *exc) {
  80. &hellip;
  81. @throw expr;
  82. &hellip;
  83. @throw;
  84. &hellip;
  85. }
  86. @catch (AnotherClass *exc) {
  87. &hellip;
  88. }
  89. @catch (id allOthers) {
  90. &hellip;
  91. }
  92. @finally {
  93. &hellip;
  94. @throw expr;
  95. &hellip;
  96. }
  97. </pre></div>
  98. <p>The <code>@throw</code> statement may appear anywhere in an Objective-C or
  99. Objective-C++ program; when used inside of a <code>@catch</code> block, the
  100. <code>@throw</code> may appear without an argument (as shown above), in
  101. which case the object caught by the <code>@catch</code> will be rethrown.
  102. </p>
  103. <p>Note that only (pointers to) Objective-C objects may be thrown and
  104. caught using this scheme. When an object is thrown, it will be caught
  105. by the nearest <code>@catch</code> clause capable of handling objects of
  106. that type, analogously to how <code>catch</code> blocks work in C++ and
  107. Java. A <code>@catch(id &hellip;)</code> clause (as shown above) may also
  108. be provided to catch any and all Objective-C exceptions not caught by
  109. previous <code>@catch</code> clauses (if any).
  110. </p>
  111. <p>The <code>@finally</code> clause, if present, will be executed upon exit
  112. from the immediately preceding <code>@try &hellip; @catch</code> section.
  113. This will happen regardless of whether any exceptions are thrown,
  114. caught or rethrown inside the <code>@try &hellip; @catch</code> section,
  115. analogously to the behavior of the <code>finally</code> clause in Java.
  116. </p>
  117. <p>There are several caveats to using the new exception mechanism:
  118. </p>
  119. <ul>
  120. <li> The <samp>-fobjc-exceptions</samp> command line option must be used when
  121. compiling Objective-C files that use exceptions.
  122. </li><li> With the GNU runtime, exceptions are always implemented as &ldquo;native&rdquo;
  123. exceptions and it is recommended that the <samp>-fexceptions</samp> and
  124. <samp>-shared-libgcc</samp> options are used when linking.
  125. </li><li> With the NeXT runtime, although currently designed to be binary
  126. compatible with <code>NS_HANDLER</code>-style idioms provided by the
  127. <code>NSException</code> class, the new exceptions can only be used on Mac
  128. OS X 10.3 (Panther) and later systems, due to additional functionality
  129. needed in the NeXT Objective-C runtime.
  130. </li><li> As mentioned above, the new exceptions do not support handling
  131. types other than Objective-C objects. Furthermore, when used from
  132. Objective-C++, the Objective-C exception model does not interoperate with C++
  133. exceptions at this time. This means you cannot <code>@throw</code> an exception
  134. from Objective-C and <code>catch</code> it in C++, or vice versa
  135. (i.e., <code>throw &hellip; @catch</code>).
  136. </li></ul>
  137. <hr>
  138. <div class="header">
  139. <p>
  140. Next: <a href="Synchronization.html#Synchronization" accesskey="n" rel="next">Synchronization</a>, Previous: <a href="compatibility_005falias.html#compatibility_005falias" accesskey="p" rel="prev">compatibility_alias</a>, Up: <a href="Objective_002dC.html#Objective_002dC" accesskey="u" rel="up">Objective-C</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  141. </div>
  142. </body>
  143. </html>