释义 |
- Example
- References
- External links
Exception safety guarantees, originally formalized by David Abrahams,[1][2] are a set of contractual guidelines that class library implementers and clients can use when reasoning about exception handling safety in any programming language that uses exceptions, particularly C++. There are several levels of exception safety (in decreasing order of safety):[3] - No-throw guarantee, also known as failure transparency: Operations are guaranteed to succeed and satisfy all requirements even in exceptional situations. If an exception occurs, it will be handled internally and not observed by clients.
- Strong exception safety, also known as commit or rollback semantics: Operations can fail, but failed operations are guaranteed to have no side effects, so all data retains their original values.&91;4&93;
- Basic exception safety, also known as a no-leak guarantee: Partial execution of failed operations can cause side effects, but all invariants are preserved and there are no resource leaks (including memory leaks). Any stored data will contain valid values, even if they differ from what they were before the exception.
- No exception safety: No guarantees are made.
Usually, at least basic exception safety is required to write robust code in such languages. Higher levels of safety can sometimes be difficult to achieve, and might incur an overhead due to extra copying. A key mechanism for exception safety is a finally clause, or similar exception handling syntax, which ensure that certain code is always run when a block is exited, including by exceptions. Several languages have constructs that simplify this, notably using the dispose pattern, named as using , with , or try -with-resources. Example Consider a smart vector type, such as C++'s {{Cpp|std::vector}} or Java's {{Cpp|ArrayList}}. When an item {{Cpp|x}} is added to a vector {{Cpp|v}}, the vector must actually add {{Cpp|x}} to the internal list of objects and update a count field that says how many objects are in {{Cpp|v}}. It may also need to allocate new memory if the existing capacity isn't sufficient. Exception safety alternatives: - No-throw guarantee
- Impossible to implement - you never can guarantee that your resources (memory/disk space) are sufficient.
- Strong exception safety
- Can be implemented fairly easily by doing any allocation first and then copying into a temporary buffer that is eventually swapped if no errors are encountered. In this case, insertion of {{Cpp|x}} into {{Cpp|v}} will either succeed, or {{Cpp|v}} will remain unchanged.
- Basic exception safety
- Implemented by ensuring that the size field is guaranteed to be updated if {{Cpp|x}} is successfully inserted. Also, all allocations need to be handled in a way that prevents any chance of a resource leak, regardless of failure.
- No exception safety
- Implementation in which an insertion failure might lead to corrupted content in {{Cpp|v}}, an incorrect size value, or a resource leak.
References 1. ^{{cite web |url=http://www.boost.org/community/exception_safety.html|title=Exception-Safety in Generic Components|accessdate=2008-08-29}} 2. ^{{Cite conference| last1 = Abrahams | first1 = D. | author1-link = David Abrahams (computer programmer)| title = Exception-Safety in Generic Components| doi = 10.1007/3-540-39953-4_6| work = Generic Programming| series = LNCS| volume = 1766| publisher = Springer| pages = 69–79| year = 2000| isbn = 978-3-540-41090-4}} 3. ^{{cite web|url=http://www.stroustrup.com/3rd_safe.pdf|author=Bjarne Stroustrup|title=Appendix E: Standard-Library Exception Safety in "The C++ Programming Language" (3rd Edition).Addison-Wesley, | isbn = 0-201-88954-4}} 4. ^http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1077.asc
External links - Herb Sutter: Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions, 2000
- Jon Kalb: Exception-Safe Coding in C++, with C++Now! 2012 presentations on exception safety.
- Related discussion on Stackoverflow: [https://stackoverflow.com/questions/1853243/c-do-you-really-write-exception-safe-code C++: do you (really) write exception safe code]
3 : C++|Control flow|Programming language topics |