[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/language/throw [Back]  [Original]

throw cppreference.com
cppreference.com

throw

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
(lvalue, rvalue, xvalue)
( )
-(C++11)
,
(C++11)
(C++11)
:
:  
:
:
:
:
:
a=b, a+=b, a-=b, a*=b, a/=b, a%=b, a&=b, a|=b, a^=b, a<<=b, a>>=b
++a, --a, a++, a--
+a, -a, a+b, a-b, a*b, a/b, a%b, ~a, a&b, a|b, a^b, a<<b, a>>b
a||b, a&&b, !a
a==b, a!=b, a<b, a>b, a<=b, a>=b, a<=>b ( C++20)
a[b], *a, &a, a->b, a.b, a->*b, a.*b
a(...), a,b, a?b:c
new
delete
throw
alignof
sizeof
sizeof...(C++11)
typeid
noexcept(C++11)
(C++17)
(C++20)
const_cast
static_cast
reinterpret_cast
dynamic_cast
: (T)a, T(a), auto(a), auto{a} ( C++23)
 
 

.

throw (1)
throw (2)

try-catch ( ) try catch
1) -,
  • rvalue. , lvalue , .
( C++11)
( C++17)
  • / ( C++11)
, , try, .
2) . catch ( catch try: ), : . , ( std::terminate, ). catch, -try-, , .

std::terminate std::unexpected ( C++17) , .

, throw.

. cv- (, ), , ).

, , (, cv-) void, .

, / ( C++11) , .

, lvalue catch, lvalue, .

, catch, ( , catch), std::exception_ptr, ( std::exception_ptr.

, ( ), try, catch , ( try-catch). , try . , catch.

, , , try, . , return, , .

() ( ), . union- , , .

, .

( C++11)

, new, , .

.

- , , , std::terminate. , , , ( ) catch .

, , std::thread, main , std::terminate. , .

, () , :

try
{
    std::string("abc").substr(10); //  std::out_of_range
}
catch (const std::exception& e)
{
    std::cout << e.what() << '\n';
//  throw e; //       std::exception
    throw;   //      std::out_of_range
}

throw prvalue void. , , :

double f(double d)
{
    return d > 1e7 ? throw std::overflow_error(" ") : d;
}

int main()  
{
    try
    {
        std::cout << f(1e10) << '\n';
    }
    catch (const std::overflow_error& e)
    {
        std::cout << e.what() << '\n';
    }
}

throw

#include <iostream>
#include <stdexcept>

struct A
{
    int n;
    
    A(int n = 0): n(n) { std::cout << "A(" << n << ")  \n"; }
    ~A() { std::cout << "A(" << n << ") \n"; }
};

int foo()
{
    throw std::runtime_error("");
}

struct B
{
    A a1, a2, a3;
    
    B() try : a1(1), a2(foo()), a3(3)
    {
        std::cout << "B  \n";
    }
    catch(...)
    {
    	std::cout << "B::B()  \n";
    }
    
    ~B() { std::cout << "B \n"; }
};

struct C : A, B
{
    C() try
    {
        std::cout << "C::C()  \n";
    }
    catch(...)
    {
        std::cout << "C::C()  \n";
    }
    
    ~C() { std::cout << "C \n"; }
};

int main () try
{
    //    A
    //   a1  B
    //     a2  B
    //    a1  B
    //     A
    C c;
}
catch (const std::exception& e)
{
    std::cout << "main()    C : " << e.what();
}

:

A(0)  
A(1)  
A(1) 
B::B()  
A(0) 
C::C()  
main()    C : 

C++:

CWG 499 C++98 ,
,
-

CWG 668 C++98 std::terminate ,

std::terminate
CWG 1863 C++11

,
CWG 1866 C++98
CWG 2176 C++98


CWG 2699 C++98 throw "EX" char*, const char*
CWG 2711 C++98 -


Web Proxy Viewer  |  New URL  |  Original Page