Type Resolution in Exception Handling

When an exception is thrown at C++and the stack is unwound, as shown in the correct handler (sentence catch) to handle the exception?

void f1()
{
    throw 1;
}

void f2()
{
    try
    {
        f1();
    }
    catch(const char* e)
    {
        std::cout << "exc1";
    }
}

...
try
{
    f2();
}
catch(int& e)
{
    std::cout << "exc2";
}
...

For example, this code unsurprisingly prints "exc2"because it catch(int& e)is capable of handling a typed object 1 int.

I do not understand how this can be solved statically? Or is it allowed dynamically? Is type information disseminated with exception?

+6
source share
3 answers

As in most cases, the C ++ standard does not specify an implementation, but limits valid implementations. There will be no universal answer that knows about the specifics.

Itanium ABI - ABI, . API , , , catch. ; , , , . (GCC , , " ".) , Itanium ABI, , , , .

. , 32- Windows , . , EXCEPTION_RECORD, , . , .

Itanium ABI, , ++. , Apple, Objective-C Itanium ABI . , catch-all .

+3

, " ", . throw -, /, catch , , / :

, :

  • E T ( cv- T)

  • T lvalue (, cv-qualified) E

  • T E

  • T E

  • T (, cv-qualified) U const U & ( ++ 14), U - ( ++ 17), E ( ++ 17), U

    • , , ,
    • ( ++ 17)
    • T - const ( ++ 14), E - std::nullptr_t.

, catch-all (catch (...)), , - . catch, .

+1

​​, RTTY , .

:

https://godbolt.org/g/dyheBZ

(gcc) typeinfo .rodata , throw catch:

    .size   typeinfo for A, 16
typeinfo for A:
    .quad   vtable for __cxxabiv1::__class_type_info+16
    .quad   typeinfo name for A
    .weak   typeinfo name for A
    .section        .rodata._ZTS1A,"aG",@progbits,typeinfo name for A,comdat
    .type   typeinfo name for A, @object
    .size   typeinfo name for A, 3
typeinfo name for A:
    .string "1A"
    .text
    .type   __static_initialization_and_destruction_0(int, int), @function

throw A(); :

    mov     edi, 1
    call    __cxa_allocate_exception
    mov     edx, 0
    mov     esi, OFFSET FLAT:typeinfo for A
    mov     rdi, rax
    call    __cxa_throw
0

All Articles