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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [libstdc++-v3/] [doc/] [xml/] [manual/] [using_exceptions.xml] - Blame information for rev 826

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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