Strong Exception Safety (8a) Flashcards
What is an exception?
Design every function to complete successfully or fail in a well-defined manner
What is the noexcept specification?
Functions that guarantee they will not throw an exception
random: returns true at compile time if(noexcept)
What is strong exception safety?
General:
Never let go of a piece of information before replacement info is ready for use
Leave objects in a valid state when throwing and re-throwing exception
Use: try-blocks, RAII
What is Mutex?
Mutual exclusion object
What we need to know:
Makes sure that multiple threads are not using the same resource at once (locks and unlocks)
What are the 4 different types of guarantees for exception safe functions?
1) No safety Guarantee
2) Basic guarantee: if an exception is thrown, everything in the function remains in a valid state
3) Strong guarantee: if an exception is thrown, the state of the program is unchanged
4) Nothrow guarantee: always works as designed and never throws exceptions
What does copy and swap do?
Copy original object, modify, if modifications are successful –> swap
exception safe way to alter object
What is RAII?
- Only code guaranteed to be executed after an exception is thrown are the destructors of objects on the stack
- Resource are tied to the lifespan of the objects
- Vital so that resources are released before permitting exceptions to propagate
What is the golden rule of exception handling?
When an exception is propagated, try to leave the object in the state it had when the function was called
If not try to make the state as good as possible or call destructors