From 07b1193de0d90640827f05a4233f11c0cb721469 Mon Sep 17 00:00:00 2001 From: Matthew Guidry Date: Mon, 16 Dec 2019 20:37:24 -0600 Subject: [PATCH] Add section about noexcept specifier (#72) * Add section about noexcept --- CPP11.md | 22 ++++++++++++++++++++++ README.md | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/CPP11.md b/CPP11.md index 18e7488..1f86b18 100644 --- a/CPP11.md +++ b/CPP11.md @@ -33,6 +33,7 @@ C++11 includes the following new language features: - [right angle brackets](#right-angle-brackets) - [ref-qualified member functions](#ref-qualified-member-functions) - [trailing return types](#trailing-return-types) +- [noexcept specifier](#noexcept-specifier) C++11 includes the following new library features: - [std::move](#stdmove) @@ -659,6 +660,27 @@ auto add(T a, U b) -> decltype(a + b) { ``` In C++14, `decltype(auto)` can be used instead. +### Noexcept specifier +The `noexcept` specifier specifies whether a function could throw exceptions. It is an improved version of `throw()`. + +```c++ +void func1() noexcept; // does not throw +void func2() noexcept(true); // does not throw +void func3() throw(); // does not throw + +void func4() noexcept(false); // may throw +``` + +Non-throwing functions are permitted to call potentially-throwing functions. Whenever an exception is thrown and the search for a handler encounters the outermost block of a non-throwing function, the function std::terminate is called. + +```c++ +extern void f(); // potentially-throwing +void g() noexcept { + f(); // valid, even if f throws + throw 42; // valid, effectively a call to std::terminate +} +``` + ## C++11 Library Features ### std::move diff --git a/README.md b/README.md index c645ec3..bd8cef6 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ C++11 includes the following new language features: - [right angle brackets](#right-angle-brackets) - [ref-qualified member functions](#ref-qualified-member-functions) - [trailing return types](#trailing-return-types) +- [noexcept specifier](#noexcept-specifier) C++11 includes the following new library features: - [std::move](#stdmove) @@ -1483,6 +1484,27 @@ auto add(T a, U b) -> decltype(a + b) { ``` In C++14, [decltype(auto)](#decltypeauto) can be used instead. +### Noexcept specifier +The `noexcept` specifier specifies whether a function could throw exceptions. It is an improved version of `throw()`. + +```c++ +void func1() noexcept; // does not throw +void func2() noexcept(true); // does not throw +void func3() throw(); // does not throw + +void func4() noexcept(false); // may throw +``` + +Non-throwing functions are permitted to call potentially-throwing functions. Whenever an exception is thrown and the search for a handler encounters the outermost block of a non-throwing function, the function std::terminate is called. + +```c++ +extern void f(); // potentially-throwing +void g() noexcept { + f(); // valid, even if f throws + throw 42; // valid, effectively a call to std::terminate +} +``` + ## C++11 Library Features ### std::move