Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the C++ runtime determine the type of a thrown exception?

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?
{
  // ..
}

See also:

How is the C++ exception handling runtime implemented?

like image 420
codymanix Avatar asked Jun 14 '09 17:06

codymanix


2 Answers

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

A handler is a match for an exception object of type E if

  • The handler is of type cv T or cv T& and E and T are the same type (ignoring the top-level cv-qualifiers), or
  • the handler is of type cv T or cv T& and T is an unambiguous public base class of E, or
  • the handler is of type cv1 T* cv2 and E is a pointer type that can be converted to the type of the handler by either or both of
    • a standard pointer conversion (4.10) not involving conversions to pointers to private or protected or ambiguous classes
    • a qualification conversion

[Note: a throw-expression which is an integral constant expression of integer type that evaluates to zero does not match a handler of pointer type; that is, the null pointer constant conversions (4.10, 4.11) do not apply. ]

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 throw statement (some time ago, i had fun figuring this out in GCC). So it does not need to do runtime type identification. So it happens, with g++, that at the side where the throw appears, it hands over a std::type_info object 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

myexceptiontype e

and

const myexceptiontype *e

Do not handle the same type, of course.

like image 150
Johannes Schaub - litb Avatar answered Nov 03 '22 17:11

Johannes Schaub - litb


There is no specification for the implementation. An implementation has to follow the standard and there are no restrictions on how it accomplishes that. And there is more than one implementation, even for one compiler.

You can read an example of one of such implementations here:

Itanium C++ ABI: Exception Handling

like image 41
PaV Avatar answered Nov 03 '22 18:11

PaV