Question: When an exception is thrown in `C++` and the stack is unwound, how is is the correct handler (`catch` clause) picked 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 `catch(int& e)` is capable of handling the `1` `int` typed object.

What I don't understand is, how can this be resolved statically? Or is it resolved dynamically? Is type information propagated with the exception?

Answer: 
As for most things, the C++ standard doesn't specify an implementation, but constrains valid implementations. There won't be a universal answer that knows about the specifics.

The Itanium ABI is a popular ABI that provides language-agnostic exception support. In that implementation, the unwinding API invokes the *personality function* of the stack frame, which receives the exception context, an exception structure and a reference to the exception handling table that guides catch behavior. The personality function is looked up based on the call's return address in the program's exception tables; it is assumed that the only instructions that can initiate an exception are call instructions. (GCC has extensions that allow to throw from signal handlers by enabling "non-call exceptions".) Compilers that use the Itanium ABI provide a personality function that knows how to check the runtime type of exception objects, and they compare that type to elements of the exception table.

There are other ways to do that. For instance, on 32-bit Windows, exception handling works by setting up handler functions, on the stack, as linked list items. These linked list nodes contain the address of exception handlers, and they use an [EXCEPTION\_RECORD](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363082(v=vs.85).aspx) to figure out where to go from there. I'm a bit sparse on the details myself, unfortunately.

The Itanium ABI, by the way, is not exclusive to C++. For instance, on Apple platforms, Objective-C code uses the Itanium ABI for exceptions as well. This has the interesting property that a catch-all clause in either language will catch exceptions from the other language.
As some answers and comments have stated, the compiler does generate runtime type information RTTI for exception types that the exception dispatcher uses to match the right handler to an exception object.

As you can see from a modified version of my example here:

<https://godbolt.org/g/dyheBZ>

The compiler (gcc) generates a `typeinfo` block in section `.rodata` for objects that are used in `throw` or `catch` statements:

```
    .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

```

And the `throw A();` statement uses this information to throw the exception:

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

```