From 999da43581a7ed516745c2cae564083583d81c77 Mon Sep 17 00:00:00 2001 From: bkoz Date: Thu, 15 Oct 2009 02:51:30 +0000 Subject: 2009-10-14 Benjamin Kosnik * doc/xml/authors.xml: Update. * doc/xml/manual/intro.xml: Move test section... * doc/xml/manual/appendix_porting.xml: ...here. * doc/xml/manual/diagnostics.xml: Edit. * doc/xml/manual/using.xml: Break out exception section. * doc/xml/manual/using_exceptions.xml: New. * doc/html: Regenerate. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@152797 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/doc/xml/manual/using_exceptions.xml | 579 +++++++++++++++++++++++ 1 file changed, 579 insertions(+) create mode 100644 libstdc++-v3/doc/xml/manual/using_exceptions.xml (limited to 'libstdc++-v3/doc/xml/manual/using_exceptions.xml') diff --git a/libstdc++-v3/doc/xml/manual/using_exceptions.xml b/libstdc++-v3/doc/xml/manual/using_exceptions.xml new file mode 100644 index 00000000000..09c21d41ccc --- /dev/null +++ b/libstdc++-v3/doc/xml/manual/using_exceptions.xml @@ -0,0 +1,579 @@ + + + + + + + C++ + + + exception + + + error + + + exception neutrality + + + exception safety + + + exception propagation + + + -fno-exceptions + + + + +Exceptions + + +The C++ language provides language support for stack unwinding +with try and catch blocks and +the throw keyword. + + + +These are very powerful constructs, and require some thought when +applied to the standard library in order to yield components that work +efficiently while cleaning up resources when unexpectedly killed via +exceptional circumstances. + + + +Two general topics of discussion follow: +exception neutrality and exception safety. + + + + +Exception Safety + + + What is exception-safe code? + + + + Will define this as reasonable and well-defined behavior by classes + and functions from the standard library when used by user-defined + classes and functions that are themselves exception safe. + + + + Please note that using exceptions in combination with templates + imposes an additional requirement for exception + safety. Instantiating types are required to have destructors that + do no throw. + + + + Using the layered approach from Abrahams, can classify library + components as providing set levels of safety. These will be called + exception guarantees, and can be divided into three categories. + + + + + + + One. Don't throw. + + + As specified in 23.2.1 general container requirements. Applicable + to container and string classes. + + + Member + functions erase, pop_back, pop_front, swap, clear. And iterator + copy constructor and assignment operator. + + + + + + Two. Don't leak resources when exceptions are thrown. This is + also referred to as the basic exception safety guarantee. + + + + This applicable throughout the standard library. + + + + + + Three. Commit-or-rollback semantics. This is + referred to as strong exception safety guarantee. + + + + As specified in 23.2.1 general container requirements. Applicable + to container and string classes. + + + Member functions insert of a single + element, push_back, push_front, + and rehash. + + + + + + + + + +Exception Neutrality + + Simply put, once thrown an exception object should continue in + flight unless handled explicitly. In practice, this means + propagating exceptions should not be swallowed in + gratuitous catch(...) blocks. Instead, + matching try and catch + blocks should have specific catch handlers and allow un-handed + exception objects to propagate. If a + terminating catch(...) blocks exist then it + should end with a throw to re-throw the current + exception. + + + + Why do this? + + + + By allowing exception objects to propagate, a more flexible + approach to error handling is made possible (although not + required.) Instead of dealing with an error immediately, one can + allow the exception to propagate up until sufficient context is + available and the choice of exiting or retrying can be made in an + informed manner. + + + + Unfortunately, this tends to be more of a guideline than a strict + rule as applied to the standard library. As such, the following is + a list of known problem areas where exceptions are not propagated. + + + + + + Input/Output + + + The destructor ios_base::Init::~Init() + swallows all exceptions from flush called on + all open streams at termination. + + + + All formatted input in basic_istream or + formatted output in basic_ostream can be + configured to swallow exceptions + when exceptions is set to + ignore ios_base::badbit. + + + + Functions that have been registered + with ios_base::register_callback swallow all + exceptions when called as part of a callback event. + + + + When closing the underlying + file, basic_filebuf::close will swallow + (non-cancellation) exceptions thrown and return NULL. + + + + + Thread + + + The constructors of thread that take a + callable function argument swallow all exceptions resulting from + executing the function argument. + + + + + + + +Doing without + + C++ is a language that strives to be as efficient as is possible + in delivering features. As such, considerable care is used by both + language implementer and designers to make sure unused features + not impose hidden or unexpected costs. The GNU system tries to be + as flexible and as configurable as possible. So, it should come as + no surprise that GNU C++ provides an optional language extension, + spelled -fno-exceptions, as a way to excise the + implicitly generated magic necessary to + support try and catch blocks + and thrown objects. (Language support + for -fno-exceptions is documented in the GNU + GCC manual.) + + + Before detailing the library support + for -fno-exceptions, first a passing note on + the things lost when this flag is used: it will break exceptions + trying to pass through code compiled + with -fno-exceptions whether or not that code + has any try or catch + constructs. If you might have some code that throws, you shouldn't + use -fno-exceptions. If you have some code that + uses try or catch, you + shouldn't use -fno-exceptions. + + + + And what it to be gained, tinkering in the back alleys with a + language like this? Exception handling overhead can be measured + in the size of the executable binary, and varies with the + capabilities of the underlying operating system and specific + configuration of the C++ compiler. On recent hardware with GNU + system software of the same age, the combined code and data size + overhead for enabling exception handling is around 7%. Of course, + if code size is of singular concern than using the appropriate + optimizer setting with exception handling enabled + (ie, -Os -fexceptions) may save up to twice + that, and preserve error checking. + + + + So. Hell bent, we race down the slippery track, knowing the brakes + are a little soft and that the right front wheel has a tendency to + wobble at speed. Go on: detail the standard library support + for -fno-exceptions. + + + + In sum, valid C++ code with exception handling is transformed into + a dialect without exception handling. In detailed steps: all use + of the C++ + keywords try, catch, + and throw in the standard library have been + permanently replaced with the pre-processor controlled equivalents + spelled __try, __catch, + and __throw_exception_again. They are defined + as follows. + + + +#ifdef __EXCEPTIONS +# define __try try +# define __catch(X) catch(X) +# define __throw_exception_again throw +#else +# define __try if (true) +# define __catch(X) if (false) +# define __throw_exception_again +#endif + + + + In addition, for every object derived from + class exception, there exists a corresponding + function with C language linkage. An example: + + + +#ifdef __EXCEPTIONS + void __throw_bad_exception(void) + { throw bad_exception(); } +#else + void __throw_bad_exception(void) + { abort(); } +#endif + + + + The last language feature needing to be transformed + by -fno-exceptions is treatment of exception + specifications on member functions. Fortunately, the compiler deals + with this by ignoring exception specifications and so no alternate + source markup is needed. + + + + By using this combination of language re-specification by the + compiler, and the pre-processor tricks and the functional + indirection layer for thrown exception objects by the library, + libstdc++ files can be compiled + with -fno-exceptions. + + + + User code that uses C++ keywords + like throw, try, + and catch will produce errors even if the user + code has included libstdc++ headers and is using constructs + like basic_iostream. Even though the standard + library has been transformed, user code may need modification. User + code that attempts or expects to do error checking on standard + library components compiled with exception handling disabled should + be evaluated and potentially made conditional. + + + + Some issues remain with this approach (see bugzilla entry + 25191). Code paths are not equivalent, in + particular catch blocks are not evaluated. Also + problematic are throw expressions expecting a + user-defined throw handler. Known problem areas in the standard + library include using an instance + of basic_istream + with exceptions set to specific + ios_base::iostate conditions, or + cascading catch blocks that dispatch error + handling or recovery efforts based on the type of exception object + thrown. + + + + Oh, and by the way: none of this hackery is at all + special. (Although perhaps well-deserving of a raised eyebrow.) + Support continues to evolve and may change in the future. Similar + and even additional techniques are used in other C++ libraries and + compilers. + + + + C++ hackers with a bent for language and control-flow purity have + been successfully consoled by grizzled C veterans lamenting the + substitution of the C language keyword + const with the uglified + doppelganger __const. + + + + + + +Compatibility + + +With <literal>C</literal> + + C language code that is expecting to interoperate with C++ should be + compiled with -fexceptions. This will make + debugging a C language function called as part of C++-induced stack + unwinding possible. + + + + In particular, unwinding into a frame with no exception handling +data will cause a runtime abort. If the unwinder runs out of unwind +info before it finds a handler, std::terminate() +is called. + + + + Please note that most development environments should take care of + getting these details right. For GNU systems, all appropriate parts + of the GNU C library are already compiled + with -fexceptions. + + + + + +With <literal>POSIX</literal> thread cancellation + + + GNU systems re-use some of the exception handling mechanisms to + track control flow for POSIX thread cancellation. + + + + Cancellation points are functions defined by POSIX as worthy of + special treatment. The standard library may use some of these + functions to implement parts of the ISO C++ standard or depend on + them for extensions. + + + + Of note: + + + + nanosleep, + read, write, open, close, + and wait. + + + + The parts of libstdc++ that use C library functions marked as + cancellation points should take pains to be exception neutral. + Failing this, catch blocks have been augmented to + show that the POSIX cancellation object is in flight. + + + + This augmentation adds a catch block + for __cxxabiv1::__forced_unwind, which is the + object representing the POSIX cancellation object. Like so: + + + + catch(const __cxxabiv1::__forced_unwind&) + { + this->_M_setstate(ios_base::badbit); + throw; + } + catch(...) + { this->_M_setstate(ios_base::badbit); } + + + + + + + +Bibliography + + + + System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008) + + + 2.9.5 Thread Cancellation + + + + 2008 + + The Open Group/The Institute of Electrical and Electronics Engineers, Inc. + + + + + + + + + + + + Error and Exception Handling + + + David + Abrahams + + + + Boost + + + + + + + + + + + + Exception-Safety in Generic Components + + + David + Abrahams + + + + Boost + + + + + + + + + + + Standard Library Exception Policy + + + Matt + Austern + + + + WG21 N1077 + + + + + + + + + + + ia64 c++ abi exception handling + + + Richard + Henderson + + + + GNU + + + + + + + + + + + Appendix E: Standard-Library Exception Safety + + + Bjarne + Stroustrup + + + + + + + + + + Exceptional C++ + + + Exception-Safety Issues and Techniques + + + Herb + Sutter + + + + + + exception_defines.h #defines try/catch + + + GCC Bug 25191 + + + + + + + + -- cgit v1.2.1 From e759020a241c1fcd30759e4e50156104fb166abf Mon Sep 17 00:00:00 2001 From: bkoz Date: Mon, 22 Feb 2010 22:52:11 +0000 Subject: 2010-02-22 Benjamin Kosnik * doc/xml/class.txml: Adjust biblio markup. * doc/xml/manual/allocator.xml: Same. * doc/xml/manual/ctype.xml: Same. * doc/xml/manual/codecvt.xml: Same. * doc/xml/manual/backwards_compatibility.xml: Same. * doc/xml/manual/abi.xml: Same. * doc/xml/manual/shared_ptr.xml: Same. * doc/xml/manual/profile_mode.xml: Same. * doc/xml/manual/using_exceptions.xml: Same. * doc/xml/manual/locale.xml: Same. * doc/xml/manual/appendix_contributing.xml: Same. * doc/xml/manual/messages.xml: Same. * doc/Makefile.am (DBLATEX_FLAGS): Adjust. * doc/Makefile.in: Regenerate. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@156980 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/doc/xml/manual/using_exceptions.xml | 134 +++++++++++------------ 1 file changed, 66 insertions(+), 68 deletions(-) (limited to 'libstdc++-v3/doc/xml/manual/using_exceptions.xml') diff --git a/libstdc++-v3/doc/xml/manual/using_exceptions.xml b/libstdc++-v3/doc/xml/manual/using_exceptions.xml index 09c21d41ccc..d6f52a572a1 100644 --- a/libstdc++-v3/doc/xml/manual/using_exceptions.xml +++ b/libstdc++-v3/doc/xml/manual/using_exceptions.xml @@ -39,7 +39,7 @@ the throw keyword. These are very powerful constructs, and require some thought when applied to the standard library in order to yield components that work efficiently while cleaning up resources when unexpectedly killed via -exceptional circumstances. +exceptional circumstances. @@ -51,8 +51,8 @@ exception neutrality and exception safety. Exception Safety - - What is exception-safe code? + + What is exception-safe code? @@ -230,9 +230,9 @@ exception neutrality and exception safety. use -fno-exceptions. If you have some code that uses try or catch, you shouldn't use -fno-exceptions. - + - + And what it to be gained, tinkering in the back alleys with a language like this? Exception handling overhead can be measured in the size of the executable binary, and varies with the @@ -318,7 +318,7 @@ exception neutrality and exception safety. library has been transformed, user code may need modification. User code that attempts or expects to do error checking on standard library components compiled with exception handling disabled should - be evaluated and potentially made conditional. + be evaluated and potentially made conditional. @@ -360,7 +360,7 @@ exception neutrality and exception safety. With <literal>C</literal> - + C language code that is expecting to interoperate with C++ should be compiled with -fexceptions. This will make debugging a C language function called as part of C++-induced stack @@ -386,7 +386,7 @@ is called. With <literal>POSIX</literal> thread cancellation - + GNU systems re-use some of the exception handling mechanisms to track control flow for POSIX thread cancellation. @@ -439,30 +439,33 @@ is called. Bibliography - - System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008) - + + + + System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008) + + + 2.9.5 Thread Cancellation - 2008 - The Open Group/The Institute of Electrical and Electronics Engineers, Inc. + The Open Group/The Institute of Electrical and Electronics + Engineers, Inc. + + - - + + + + + Error and Exception Handling + - - - - - - Error and Exception Handling - David Abrahams @@ -472,17 +475,17 @@ is called. Boost - - - - - + - - Exception-Safety in Generic Components - + + + + Exception-Safety in Generic Components + + + David Abrahams @@ -492,16 +495,16 @@ is called. Boost - - - - - + - - Standard Library Exception Policy - + + + + Standard Library Exception Policy + + + Matt Austern @@ -511,16 +514,16 @@ is called. WG21 N1077 - - - - - + - - ia64 c++ abi exception handling - + + + + ia64 c++ abi exception handling + + + Richard Henderson @@ -530,25 +533,21 @@ is called. GNU - - - - - + - - Appendix E: Standard-Library Exception Safety - + + + + Appendix E: Standard-Library Exception Safety + + + Bjarne Stroustrup - - - - - + @@ -561,18 +560,17 @@ is called. <firstname>Herb</firstname> <surname>Sutter</surname> </author> - </biblioentry> + </biblioentry> <biblioentry> - <title> - exception_defines.h #defines try/catch - - - GCC Bug 25191 - - + + + + GCC Bug 25191: exception_defines.h #defines try/catch + + - + -- cgit v1.2.1 From d0cdcef958677d8b8fac1a498e7201df608a44da Mon Sep 17 00:00:00 2001 From: bkoz Date: Thu, 22 Jul 2010 22:58:15 +0000 Subject: 2010-07-22 Benjamin Kosnik DocBook 4.5 to 5.0 transition. * doc/xml/authors.xml: Update markup to DocBook 5.0. * doc/xml/faq.xml: Same. * doc/xml/api.xml: Same. * doc/xml/class.txml * doc/xml/gnu/gpl-3.0.xml: Same. * doc/xml/gnu/fdl-1.2.xml: Same. * doc/xml/gnu/fdl-1.3.xml: Same. * doc/xml/gnu/gpl-2.0.xml: Same. * doc/xml/chapter.txml: Same. * doc/xml/manual/mt_allocator.xml: Same. * doc/xml/manual/allocator.xml: Same. * doc/xml/manual/ctype.xml: Same. * doc/xml/manual/numerics.xml: Same. * doc/xml/manual/codecvt.xml: Same. * doc/xml/manual/backwards_compatibility.xml: Same. * doc/xml/manual/concurrency.xml: Same. * doc/xml/manual/intro.xml: Same. * doc/xml/manual/abi.xml: Same. * doc/xml/manual/shared_ptr.xml: Same. * doc/xml/manual/status_cxxtr1.xml: Same. * doc/xml/manual/auto_ptr.xml: Same. * doc/xml/manual/internals.xml: Same. * doc/xml/manual/atomics.xml: Same. * doc/xml/manual/parallel_mode.xml: Same. * doc/xml/manual/status_cxx1998.xml: Same. * doc/xml/manual/profile_mode.xml: Same. * doc/xml/manual/containers.xml: Same. * doc/xml/manual/io.xml: Same. * doc/xml/manual/concurrency_extensions.xml: Same. * doc/xml/manual/appendix_porting.xml: Same. * doc/xml/manual/utilities.xml: Same. * doc/xml/manual/support.xml: Same. * doc/xml/manual/bitmap_allocator.xml: Same. * doc/xml/manual/configure.xml: Same. * doc/xml/manual/build_hacking.xml: Same. * doc/xml/manual/evolution.xml: Same. * doc/xml/manual/using.xml: Same. * doc/xml/manual/using_exceptions.xml: Same. * doc/xml/manual/debug.xml: Same. * doc/xml/manual/localization.xml: Same. * doc/xml/manual/strings.xml: Same. * doc/xml/manual/debug_mode.xml: Same. * doc/xml/manual/locale.xml: Same. * doc/xml/manual/extensions.xml: Same. * doc/xml/manual/appendix_contributing.xml: Same. * doc/xml/manual/prerequisites.xml: Same. * doc/xml/manual/messages.xml: Same. * doc/xml/manual/diagnostics.xml: Same. * doc/xml/manual/algorithms.xml: Same. * doc/xml/manual/appendix_free.xml: Same. * doc/xml/manual/iterators.xml: Same. * doc/xml/manual/spine.xml: Same. * doc/xml/manual/status_cxxtr24733.xml: Same. * doc/xml/manual/status_cxx200x.xml: Same. * doc/xml/manual/test.xml: Same. * doc/xml/book.txml: Same. * doc/xml/spine.xml: Same. * doc/Makefile.am: Same. * doc/Makefile.in: Regenerate. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@162433 138bc75d-0d04-0410-961f-82ee72b054a4 --- libstdc++-v3/doc/xml/manual/using_exceptions.xml | 177 ++++++++++------------- 1 file changed, 74 insertions(+), 103 deletions(-) (limited to 'libstdc++-v3/doc/xml/manual/using_exceptions.xml') diff --git a/libstdc++-v3/doc/xml/manual/using_exceptions.xml b/libstdc++-v3/doc/xml/manual/using_exceptions.xml index d6f52a572a1..afc38734ae8 100644 --- a/libstdc++-v3/doc/xml/manual/using_exceptions.xml +++ b/libstdc++-v3/doc/xml/manual/using_exceptions.xml @@ -1,7 +1,8 @@ - +
- +Exceptions C++ @@ -25,9 +26,7 @@ -fno-exceptions - - -Exceptions + The C++ language provides language support for stack unwinding @@ -48,8 +47,8 @@ exception neutrality and exception safety. - -Exception Safety +
Exception Safety + What is exception-safe code? @@ -121,11 +120,11 @@ exception neutrality and exception safety. - +
- -Exception Neutrality +
Exception Neutrality + Simply put, once thrown an exception object should continue in flight unless handled explicitly. In practice, this means @@ -201,10 +200,10 @@ exception neutrality and exception safety. - +
+ +
Doing without - -Doing without C++ is a language that strives to be as efficient as is possible in delivering features. As such, considerable care is used by both @@ -217,7 +216,7 @@ exception neutrality and exception safety. support try and catch blocks and thrown objects. (Language support for -fno-exceptions is documented in the GNU - GCC manual.) + GCC manual.) Before detailing the library support @@ -353,13 +352,13 @@ exception neutrality and exception safety. - +
- -Compatibility +
Compatibility + + +
With <literal>C</literal> - -With <literal>C</literal> C language code that is expecting to interoperate with C++ should be compiled with -fexceptions. This will make @@ -381,10 +380,10 @@ is called. with -fexceptions. - +
+ +
With <literal>POSIX</literal> thread cancellation - -With <literal>POSIX</literal> thread cancellation GNU systems re-use some of the exception handling mechanisms to @@ -424,28 +423,27 @@ is called. catch(const __cxxabiv1::__forced_unwind&) { - this->_M_setstate(ios_base::badbit); + this->_M_setstate(ios_base::badbit); throw; } catch(...) - { this->_M_setstate(ios_base::badbit); } + { this->_M_setstate(ios_base::badbit); } - - +
+
+ +Bibliography - -Bibliography - - - - System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008) - - - + + + + System Interface Definitions, Issue 7 (IEEE Std. 1003.1-2008) + + 2.9.5 Thread Cancellation @@ -459,17 +457,13 @@ is called. - - - - Error and Exception Handling - - - - - David - Abrahams - + + + + Error and Exception Handling + + + DavidAbrahams Boost @@ -479,17 +473,13 @@ is called. - - - - Exception-Safety in Generic Components - - - - - David - Abrahams - + + + + Exception-Safety in Generic Components + + + DavidAbrahams Boost @@ -498,17 +488,12 @@ is called. - - - - Standard Library Exception Policy - - - - - Matt - Austern - + + + + Standard Library Exception Policy + + MattAustern WG21 N1077 @@ -517,17 +502,13 @@ is called. - - - - ia64 c++ abi exception handling - - - - - Richard - Henderson - + + + + ia64 c++ abi exception handling + + + RichardHenderson GNU @@ -536,42 +517,32 @@ is called. - - - - Appendix E: Standard-Library Exception Safety - - - - - Bjarne - Stroustrup - + + + + Appendix E: Standard-Library Exception Safety + + BjarneStroustrup - + <citetitle> Exceptional C++ - + Exception-Safety Issues and Techniques - - Herb - Sutter - + HerbSutter - - - - GCC Bug 25191: exception_defines.h #defines try/catch - - + + + GCC Bug 25191: exception_defines.h #defines try/catch + - +
-- cgit v1.2.1