OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libstdc++-v3/] [doc/] [xml/] [manual/] [using_exceptions.xml] - Blame information for rev 745

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 742 jeremybenn
2
         xml:id="manual.intro.using.exceptions" xreflabel="Using Exceptions">
3
4
 
5
Exceptions
6
  
7
    
8
      C++
9
    
10
    
11
      exception
12
    
13
    
14
      error
15
    
16
    
17
      exception neutrality
18
    
19
    
20
      exception safety
21
    
22
    
23
      exception propagation
24
    
25
    
26
      -fno-exceptions
27
    
28
  
29
30
 
31
32
The C++ language provides language support for stack unwinding
33
with try and catch blocks and
34
the throw keyword.
35
36
 
37
38
These are very powerful constructs, and require some thought when
39
applied to the standard library in order to yield components that work
40
efficiently while cleaning up resources when unexpectedly killed via
41
exceptional circumstances.
42
43
 
44
45
Two general topics of discussion follow:
46
exception neutrality and exception safety.
47
48
 
49
 
50
Exception Safety
51
 
52
 
53
  
54
    What is exception-safe code?
55
  
56
 
57
  
58
    Will define this as reasonable and well-defined behavior by classes
59
    and functions from the standard library when used by user-defined
60
    classes and functions that are themselves exception safe.
61
  
62
 
63
  
64
    Please note that using exceptions in combination with templates
65
    imposes an additional requirement for exception
66
    safety. Instantiating types are required to have destructors that
67
    do no throw.
68
  
69
 
70
  
71
    Using the layered approach from Abrahams, can classify library
72
    components as providing set levels of safety. These will be called
73
    exception guarantees, and can be divided into three categories.
74
  
75
 
76
77
 
78
  
79
  
80
    One. Don't throw.
81
  
82
  
83
    As specified in 23.2.1 general container requirements. Applicable
84
    to container and string classes.
85
  
86
  
87
    Member
88
    functions erase, pop_back, pop_front, swap, clear. And iterator
89
    copy constructor and assignment operator.
90
  
91
  
92
 
93
  
94
  
95
    Two. Don't leak resources when exceptions are thrown. This is
96
    also referred to as the basic exception safety guarantee.
97
  
98
 
99
  
100
    This applicable throughout the standard library.
101
  
102
  
103
 
104
  
105
  
106
    Three. Commit-or-rollback semantics.  This is
107
    referred to as strong exception safety guarantee.
108
  
109
 
110
  
111
    As specified in 23.2.1 general container requirements. Applicable
112
    to container and string classes.
113
  
114
  
115
    Member functions insert of a single
116
    element, push_back, push_front,
117
    and rehash.
118
  
119
 
120
  
121
122
 
123
124
 
125
 
126
Exception Neutrality
127
 
128
  
129
    Simply put, once thrown an exception object should continue in
130
    flight unless handled explicitly. In practice, this means
131
    propagating exceptions should not be swallowed in
132
    gratuitous catch(...) blocks. Instead,
133
    matching try and catch
134
    blocks should have specific catch handlers and allow un-handed
135
    exception objects to propagate. If a
136
    terminating catch(...) blocks exist then it
137
    should end with a throw to re-throw the current
138
    exception.
139
  
140
 
141
  
142
    Why do this?
143
  
144
 
145
  
146
    By allowing exception objects to propagate, a more flexible
147
    approach to error handling is made possible (although not
148
    required.) Instead of dealing with an error immediately, one can
149
    allow the exception to propagate up until sufficient context is
150
    available and the choice of exiting or retrying can be made in an
151
    informed manner.
152
  
153
 
154
  
155
    Unfortunately, this tends to be more of a guideline than a strict
156
    rule as applied to the standard library. As such, the following is
157
    a list of known problem areas where exceptions are not propagated.
158
  
159
 
160
161
  
162
    
163
      Input/Output
164
    
165
  
166
    The destructor ios_base::Init::~Init()
167
    swallows all exceptions from flush called on
168
    all open streams at termination.
169
  
170
 
171
  
172
    All formatted input in basic_istream or
173
    formatted output in basic_ostream can be
174
    configured to swallow exceptions
175
    when exceptions is set to
176
    ignore ios_base::badbit.
177
  
178
 
179
  
180
    Functions that have been registered
181
    with ios_base::register_callback swallow all
182
    exceptions when called as part of a callback event.
183
  
184
 
185
  
186
    When closing the underlying
187
    file, basic_filebuf::close will swallow
188
    (non-cancellation) exceptions thrown and return NULL.
189
  
190
  
191
  
192
    
193
      Thread
194
    
195
    
196
      The constructors of thread that take a
197
      callable function argument swallow all exceptions resulting from
198
      executing the function argument.
199
    
200
  
201
202
 
203
204
 
205
Doing without
206
 
207
  
208
    C++ is a language that strives to be as efficient as is possible
209
    in delivering features. As such, considerable care is used by both
210
    language implementer and designers to make sure unused features
211
    not impose hidden or unexpected costs. The GNU system tries to be
212
    as flexible and as configurable as possible. So, it should come as
213
    no surprise that GNU C++ provides an optional language extension,
214
    spelled -fno-exceptions, as a way to excise the
215
    implicitly generated magic necessary to
216
    support try and catch blocks
217
    and thrown objects. (Language support
218
    for -fno-exceptions is documented in the GNU
219
    GCC manual.)
220
  
221
 
222
  Before detailing the library support
223
    for -fno-exceptions, first a passing note on
224
    the things lost when this flag is used: it will break exceptions
225
    trying to pass through code compiled
226
    with -fno-exceptions whether or not that code
227
    has any try or catch
228
    constructs. If you might have some code that throws, you shouldn't
229
    use -fno-exceptions. If you have some code that
230
    uses try or catch, you
231
    shouldn't use -fno-exceptions.
232
  
233
 
234
  
235
    And what it to be gained, tinkering in the back alleys with a
236
    language like this? Exception handling overhead can be measured
237
    in the size of the executable binary, and varies with the
238
    capabilities of the underlying operating system and specific
239
    configuration of the C++ compiler. On recent hardware with GNU
240
    system software of the same age, the combined code and data size
241
    overhead for enabling exception handling is around 7%. Of course,
242
    if code size is of singular concern than using the appropriate
243
    optimizer setting with exception handling enabled
244
    (ie, -Os -fexceptions) may save up to twice
245
    that, and preserve error checking.
246
  
247
 
248
  
249
    So. Hell bent, we race down the slippery track, knowing the brakes
250
    are a little soft and that the right front wheel has a tendency to
251
    wobble at speed. Go on: detail the standard library support
252
    for -fno-exceptions.
253
  
254
 
255
  
256
    In sum, valid C++ code with exception handling is transformed into
257
    a dialect without exception handling. In detailed steps: all use
258
    of the C++
259
    keywords try, catch,
260
    and throw in the standard library have been
261
    permanently replaced with the pre-processor controlled equivalents
262
    spelled __try, __catch,
263
    and __throw_exception_again. They are defined
264
    as follows.
265
  
266
 
267
268
#ifdef __EXCEPTIONS
269
# define __try      try
270
# define __catch(X) catch(X)
271
# define __throw_exception_again throw
272
#else
273
# define __try      if (true)
274
# define __catch(X) if (false)
275
# define __throw_exception_again
276
#endif
277
278
 
279
280
  In addition, for every object derived from
281
  class exception, there exists a corresponding
282
  function with C language linkage. An example:
283
284
 
285
286
#ifdef __EXCEPTIONS
287
  void __throw_bad_exception(void)
288
  { throw bad_exception(); }
289
#else
290
  void __throw_bad_exception(void)
291
  { abort(); }
292
#endif
293
294
 
295
296
  The last language feature needing to be transformed
297
  by -fno-exceptions is treatment of exception
298
  specifications on member functions. Fortunately, the compiler deals
299
  with this by ignoring exception specifications and so no alternate
300
  source markup is needed.
301
302
 
303
304
  By using this combination of language re-specification by the
305
  compiler, and the pre-processor tricks and the functional
306
  indirection layer for thrown exception objects by the library,
307
  libstdc++ files can be compiled
308
  with -fno-exceptions.
309
310
 
311
312
 User code that uses C++ keywords
313
 like throw, try,
314
 and catch will produce errors even if the user
315
 code has included libstdc++ headers and is using constructs
316
 like basic_iostream. Even though the standard
317
 library has been transformed, user code may need modification. User
318
  code that attempts or expects to do error checking on standard
319
  library components compiled with exception handling disabled should
320
  be evaluated and potentially made conditional.
321
322
 
323
324
  Some issues remain with this approach (see bugzilla entry
325
  25191). Code paths are not equivalent, in
326
  particular catch blocks are not evaluated. Also
327
  problematic are throw expressions expecting a
328
  user-defined throw handler. Known problem areas in the standard
329
  library include using an instance
330
  of basic_istream
331
  with exceptions set to specific
332
  ios_base::iostate conditions, or
333
  cascading catch blocks that dispatch error
334
  handling or recovery efforts based on the type of exception object
335
  thrown.
336
337
 
338
339
  Oh, and by the way: none of this hackery is at all
340
  special. (Although perhaps well-deserving of a raised eyebrow.)
341
  Support continues to evolve and may change in the future. Similar
342
  and even additional techniques are used in other C++ libraries and
343
  compilers.
344
345
 
346
347
 C++ hackers with a bent for language and control-flow purity have
348
  been successfully consoled by grizzled C veterans lamenting the
349
  substitution of the C language keyword
350
  const with the uglified
351
  doppelganger __const.
352
353
 
354
 
355
356
 
357
Compatibility
358
 
359
 
360
With <literal>C</literal>
361
 
362
363
  C language code that is expecting to interoperate with C++ should be
364
  compiled with -fexceptions. This will make
365
  debugging a C language function called as part of C++-induced stack
366
  unwinding possible.
367
368
 
369
370
  In particular, unwinding into a frame with no exception handling
371
data will cause a runtime abort. If the unwinder runs out of unwind
372
info before it finds a handler, std::terminate()
373
is called.
374
375
 
376
377
  Please note that most development environments should take care of
378
  getting these details right. For GNU systems, all appropriate parts
379
  of the GNU C library are already compiled
380
  with -fexceptions.
381
382
 
383
384
 
385
With <literal>POSIX</literal> thread cancellation
386
 
387
 
388
389
  GNU systems re-use some of the exception handling mechanisms to
390
  track control flow for POSIX thread cancellation.
391
392
 
393
394
  Cancellation points are functions defined by POSIX as worthy of
395
  special treatment. The standard library may use some of these
396
  functions to implement parts of the ISO C++ standard or depend on
397
  them for extensions.
398
399
 
400
401
  Of note:
402
403
 
404
405
  nanosleep,
406
  read, write, open, close,
407
  and wait.
408
409
 
410
411
  The parts of libstdc++ that use C library functions marked as
412
  cancellation points should take pains to be exception neutral.
413
  Failing this, catch blocks have been augmented to
414
  show that the POSIX cancellation object is in flight.
415
416
 
417
418
  This augmentation adds a catch block
419
  for __cxxabiv1::__forced_unwind, which is the
420
  object representing the POSIX cancellation object. Like so:
421
422
 
423
424
  catch(const __cxxabiv1::__forced_unwind&)
425
  {
426
    this->_M_setstate(ios_base::badbit);
427
    throw;
428
  }
429
  catch(...)
430
  { this->_M_setstate(ios_base::badbit); }
431
432
 
433
 
434
435
436
 
437
Bibliography
438
 
439
 
440
  
441
      </code></pre></td>
      </tr>
      <tr valign="middle">
         <td>442</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        <link xmlns:xlink="http://www.w3.org/1999/xlink"</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>443</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>              xlink:href="http://www.opengroup.org/austin"></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>444</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008)</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>445</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        </link></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>446</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      
447
 
448
 
449
    
450
      2.9.5 Thread Cancellation
451
    
452
    
453
      2008
454
      
455
        The Open Group/The Institute of Electrical and Electronics
456
        Engineers, Inc.
457
      
458
    
459
  
460
 
461
  
462
      </code></pre></td>
      </tr>
      <tr valign="middle">
         <td>463</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        <link xmlns:xlink="http://www.w3.org/1999/xlink"</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>464</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>              xlink:href="http://www.boost.org/community/error_handling.html"></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>465</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        Error and Exception Handling</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>466</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        </link></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>467</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      
468
 
469
    DavidAbrahams 
470
    
471
      
472
        Boost
473
      
474
    
475
  
476
 
477
 
478
  
479
      </code></pre></td>
      </tr>
      <tr valign="middle">
         <td>480</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        <link xmlns:xlink="http://www.w3.org/1999/xlink"</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>481</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>              xlink:href="http://www.boost.org/community/exception_safety.html"></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>482</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        Exception-Safety in Generic Components</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>483</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        </link></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>484</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      
485
 
486
    DavidAbrahams
487
    
488
      
489
        Boost
490
      
491
    
492
  
493
 
494
  
495
      </code></pre></td>
      </tr>
      <tr valign="middle">
         <td>496</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        <link xmlns:xlink="http://www.w3.org/1999/xlink"</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>497</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>              xlink:href="www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1077.pdf"></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>498</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        Standard Library Exception Policy</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>499</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        </link></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>500</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      
501
 
502
    MattAustern
503
    
504
      
505
        WG21 N1077
506
      
507
    
508
  
509
 
510
  
511
      </code></pre></td>
      </tr>
      <tr valign="middle">
         <td>512</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        <link xmlns:xlink="http://www.w3.org/1999/xlink"</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>513</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>              xlink:href="http://gcc.gnu.org/ml/gcc-patches/2001-03/msg00661.html"></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>514</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        ia64 c++ abi exception handling</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>515</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        </link></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>516</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      
517
 
518
    RichardHenderson
519
    
520
      
521
        GNU
522
      
523
    
524
  
525
 
526
  
527
      </code></pre></td>
      </tr>
      <tr valign="middle">
         <td>528</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        <link xmlns:xlink="http://www.w3.org/1999/xlink"</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>529</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>              xlink:href="http://www.research.att.com/~bs/3rd_safe.pdf"></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>530</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        Appendix E: Standard-Library Exception Safety</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>531</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        </link></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>532</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      
533
    BjarneStroustrup
534
  
535
 
536
  
537
    
538
      Exceptional C++
539
    
540
    
541
      Exception-Safety Issues and Techniques
542
    
543
    HerbSutter
544
  
545
 
546
  
547
      </code></pre></td>
      </tr>
      <tr valign="middle">
         <td>548</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        <link xmlns:xlink="http://www.w3.org/1999/xlink"</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>549</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>              xlink:href="http://gcc.gnu.org/PR25191"></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>550</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      GCC Bug 25191: exception_defines.h #defines try/catch</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>551</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>        </link></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>552</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>      
553
  
554
 
555
556
 
557

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.