Specifies whether a function will throw exceptions or not. Show
Contents
[edit] Syntax
1) Same as 2) If expression evaluates to true, the function is declared to not throw any exceptions. [edit] ExplanationThe noexcept-specification (just like dynamic exception specification) can appear as a part of a function declarator when declaring functions, pointer to functions, references to functions, or pointers to member function type, and also when declaring a parameter or a return type of a function that happens to be a pointer or reference to function. It cannot appear in a typedef or type alias declaration. void f() noexcept; // the function f() does not throw void (*fp)() noexcept(false); // fp points to a function that may throw void g(void pfa() noexcept); // g takes a pointer to function that doesn't throw // typedef int (*pf)() noexcept; // error The noexcept-specification is not a part of the function type. However, if any declaration of a function has a noexcept specification that isn't void f() noexcept; void f(); // error, incompatible exception specifications void g() noexcept(false); void g(); // ok If a search for a matching exception handler leaves a function marked noexcept or noexcept(true), std::terminate is called immediately. Inheriting constructors and the implicitly-declared
default constructors, copy constructors,
move constructors, destructors,
copy-assignment operators, move-assignment operators are all Any user-defined destructor is Any
deallocation function is
[edit] NotesOne of the uses of the constant expression is (along with the
Note that a [edit] Deprecates
[edit] Keywordsnoexcept [edit] Example// whether foo is declared noexcept depends on if the expression // T() will throw any exceptions template <class T> void foo() noexcept(noexcept(T())) {} void bar() noexcept(true) {} void baz() noexcept { throw 42; } // noexcept is the same as noexcept(true) int main() { foo<int>(); // noexcept(noexcept(int())) => noexcept(true), so this is fine bar(); // fine baz(); // compiles, but at runtime this calls std::terminate } [edit] See alsoDoes the throws clause cause an exception to thrown?The throws clause causes an exception to be thrown. The throw statement informs the compiler that a method throws one or more exception.
When an exception is thrown by a method that is executing?8) When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.
When you write a method that throws a checked exception you must?If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using the throws keyword. In checked exception, there are two types: fully checked and partially checked exceptions.
When catching multiple exceptions that are related to one another?When catching multiple exceptions that are related to one another through inheritance you should handle the more general exception classes before the more specialized exception classes.
|