Beispiel noexcept
noexcept.cpp — 1.5 KB
Dateiinhalt
// // main.cpp // noexcept // // Created by Klaus Ahrens on 26.11.12. // Copyright (c) 2012 Klaus Ahrens. All rights reserved. // #include <iostream> void o(const char* msg) { std::cout<<msg<<std::endl; } class X { public: X() {o("X()");} ~X() {o("~X()");} }; void foo(int n) { if (n<0) throw 666; } void nonexceptional() noexcept { X x1; foo(1); X x2; foo(0); X x3; foo(-1); } void doesnotthrow() throw() { X x1; foo(1); X x2; foo(0); X x3; foo(-1); } void exceptional() { X x1; foo(1); X x2; foo(0); X x3; foo(-1); } int main(int argc, const char * argv[]) try { nonexceptional(); } catch (...) {o("catched");} /* ISO C++11: § 15.5.1 In such cases, std::terminate() is called (18.8.3). In the situation where no matching handler is found, it is implementation-defined whether or not the stack is unwound before std::terminate() is called. In the situation where the search for a handler (15.3) encounters the outermost block of a function with a noexcept-specification that does not allow the exception (15.4), it is implementation-defined whether the stack is unwound, unwound partially, or not unwound at all before std::terminate() is called. In all other situations, the stack shall not be unwound before std::terminate() is called. An implementation is not permitted to finish stack unwinding prematurely based on a determination that the unwind process will eventually cause a call to std::terminate(). */