If I do the following, how does the runtime determine the type of the thrown exception? Does it use RTTI for that?
try
{
dostuff(); // throws something
}
catch(int e)
{
// ..
}
catch (const char * e)
{
// ..
}
catch (const myexceptiontype * e)
{
// ..
}
catch (myexceptiontype e) // is this the same as the previous handler?
{
// ..
}
Unlike the concerns asked in that other questions, the answer to this question can be answered entirely by means of the Standard. Here are the rules
As i’m not quite sure about your level of understanding of the Standard, i will leave this unexplained, and answer as you ask.
With regard to whether it uses RTTI or not – well, the type of the exception object being thrown is the static type of the expression you hand over to the
throwstatement (some time ago, i had fun figuring this out in GCC). So it does not need to do runtime type identification. So it happens, withg++, that at the side where thethrowappears, it hands over astd::type_infoobject representing the type of the exception object, the object itself and a destructor function.It’s then thrown and frames are searched for a matching handler. Using information found in big tables (located in a section called
.eh_frame), and using the return address, it looks what function is responsible for the next handling. The function will have a personality routine installed that figures out whether it can handle the exception or not. This whole procedure is described (and in more detail, of course) in the Itanium C++ ABI (implemented by G++) linked by @PaV.So, to conclude
and
Do not handle the same type, of course.