Skip to main content

Runtime Errors and Exceptions

  • Chapter
  • First Online:
Beginning C++20
  • 3109 Accesses

Abstract

Exceptions are used to signal errors or unexpected conditions in a program. While other error-handling mechanisms do exist, exceptions generally lead to simpler, cleaner code, in which you are less likely to miss an error. Particularly in combination with the RAII principle (short for “resource acquisition is initialization”), we will show that exceptions form the basis of some of the most effective programming patterns in modern C++.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 54.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

     You shouldn’t even call std::exit() from a terminate handler because that again may result in undefined behavior. Consult your Standard Library if you want to know the subtle differences between the various program termination functions.

  2. 2.

    If you’re not careful, even throwing from a noexcept(false) destructor may very well still trigger a call to std::terminate(). Details are again out of scope. Bottom line: Unless you really know what you’re doing, never throw exceptions from a destructor!

  3. 3.

    Concretely, the compiler implicitly generates the noexcept specification for a destructor without an explicit noexcept(...) specification unless the type of one of the subobjects of its class has a destructor that is not noexcept.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2020 Ivor Horton and Peter Van Weert

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Horton, I., Van Weert, P. (2020). Runtime Errors and Exceptions. In: Beginning C++20. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5884-2_16

Download citation

Publish with us

Policies and ethics