Skip to content

cpp exception

serhij.zolotukhin edited this page May 6, 2026 · 27 revisions

C++ language

Exception Safety Guarantees

These guarantees are a contract between the function and its caller, ensuring the program remains predictable and manageable even during errors. This concept is language-independent.

https://rollbar.com/blog/error-exceptions-in-c/ https://medium.com/@dnotitia/back-to-basics-error-handling-in-c-4afb20b5f98b https://www.damonyuan.com/tech/260303-strong-exception-safty https://www.opensourceforu.com/2011/06/joy-of-programming-levels-of-exception-safety/ https://towardsdev.com/cpp-exception-safety-strong-basic-guarantees-0e04901bd9bd

  • No Guarantee - An exception may leak resources or leave objects in an invalid state.
  • Basic guarantee - No resource leaks, invariants are preserved, but object value may change.
  • Strong guarantee - Operation is transactional: either full success or no visible effect.
  • No-throw guarantee - Operation never lets exceptions escape.

The classic strong-guarantee technique: copy-and-swap

Copy-and-swap is just one instance of a broader principle: do anything that can throw first, then commit with operations that can’t. The Two-Phase Pattern: Risky Work First, Commit Last

Throwing Exceptions in C++: By Value or By Pointer

https://medium.com/@bsharmarishab/throwing-exceptions-in-c-by-value-or-by-pointer-014361231e40

  • Throwing by Value: The Recommended Approach The C++ community has largely settled on throwing by value because:
  • It eliminates memory management headaches
  • It follows RAII (Resource Acquisition Is Initialization) principles
  • It’s consistent with how most C++ objects are passed around
  • It’s less prone to errors and leaks
  • Throwing by Pointer: The Problematic Approach A pointer-based exception handling is dangerous and prone to memory leaks or crashes.

Catch by Value or Reference

https://www.geeksforgeeks.org/cpp/exception-handling-c/ the catch block can catch exceptions either by value or by reference

  • Catch by Value: Catching exceptions by value creates a new copy of the thrown object in the catch block.
  • Catch by Reference: Catch by reference method just pass the reference to the exception thrown instead of creating a copy. The main advantage of this method is in catching polymorphic exception types.

Handle Uncaught Exceptions

If the exception is not caught by any of the catch block (in case catch all is not present), then by default, the exception handling subsystem aborts the execution of the program.

Best Practices

https://medium.com/@bsharmarishab/throwing-exceptions-in-c-by-value-or-by-pointer-014361231e40

Always throw by value: It’s simpler, safer, and follows the principle of least surprise. Catch by const reference: This avoids unnecessary copies while maintaining safety. Make your exception objects copyable: Since they’ll be copied during the throw process. Keep exception objects small: Large exception objects could impact performance during the copy operation.

open questions

  • std::exception

  • catch (...)

  • re throw - create a new instace of execption - when an instance is created and deleted

  • Exception Propagation (Stack Unwinding)

  • how exception works on assember level

  • Exception when call destructor

  • Throw exception in constructor and distructor

  • duble exception throw

  • are we handling an exception for now

  • noexcept

  • Best and bad pratices with excetion

  • Multithreading exception

URLs

https://www.geeksforgeeks.org/cpp/exception-handling-c/ https://devabdilah.medium.com/exception-handling-in-c-1ba1dadae4f9 https://www.linkedin.com/pulse/exception-handling-c-how-works-under-hood-compilers-sona-badalyan-9co6e/ https://akrzemi1.wordpress.com/2011/09/21/destructors-that-throw/ https://www.sandordargo.com/blog/2020/07/22/always-catch-exceptions-by-reference

My side bar

Clone this wiki locally