Type resolution during exception handling

Viewed 199

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?

3 Answers
Related