In computing, an exception is a special situation where the program cannot do things the way it usually would and is forced to do something else instead. One layer of the system uses an exception to give another layer information about special states the system is currently in. The different layers of software or hardware have contracts, that tell what can be expected; this is generally known as Programming by Contract. In the context of exception handling, a program is said to be exception-safe, if exceptions that occur will not produce side-effects (such as memory leaks), will not change stored data so that it becomes unreadable, or generate output that is invalid. There are different levels of exception safety:
- Failure transparency or no throw guarantee: No matter what happens, no exceptions will be thrown. This is the best level of exception safety, but also the most difficult to implement.
- Commit or rollback semantics, Strong exception safety, no change guarantee: Operations can fail, and exceptions will be thrown. However, a failed operation is guaranteed to not have side-effects or change the data.
- Basic exception safety: A part of the failed operation may have been executed, and can have side-effects. The state of the data may be different before and after the execution, but in both cases, the data will be in a valid state.
- Minimal exception safety,No-leak guarantee: As only a part of the operation was executed, the stored data may be invalid, afterwards. The system will keep running, and no resources get leaked, however.
- No exception safety: No guarantees can be made. This is the worst level of exception safety.
Usually, a programmer will try to catch the exception early so that problems don't get worse over time.