11

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?

Community
  • 1
  • 1
codymanix
  • 28,510
  • 21
  • 92
  • 151
  • Duplicate of http://stackoverflow.com/questions/490773/how-does-the-c-exception-handling-work and others. – Matthew Flaschen Jun 14 '09 at 17:28
  • This is no duplicate, because I want to know how the type is determined, but the other question explains how to allocate space for it and transfer control. – codymanix Jun 14 '09 at 17:41
  • The other question asked in particular about storage. But he wanted to know the general procedure, from throwing down to handling. Only the answers to that other question could be more detailed IMHO. – Johannes Schaub - litb Jun 14 '09 at 17:44
  • 1
    But i don't think this is a duplicate anymore. this answer can be entirely answered by means of the Standard, if you are really only interested how types are matched. voting for reopening, because i already had an answer finished ... – Johannes Schaub - litb Jun 14 '09 at 17:52
  • The referenced dupe is not exactly detailed - I think this should be reopened. –  Jun 14 '09 at 17:52
  • There should be a feature: If one previously voted for close, then a following vote-for-reopen should count as two votes :) – Johannes Schaub - litb Jun 14 '09 at 17:59
  • Related question: http://stackoverflow.com/questions/307610/how-do-exceptions-work-behind-the-scenes-in-c – CesarB Jun 14 '09 at 21:24
  • I was asked to merge this (dup), but from the comments it sounds like this no longer applies... correct? (my C++ is lacking, so I'm relying on the community steer here...) – Marc Gravell Jun 15 '09 at 21:58
  • @Marc: Yeah, from the look of things there's enough to differentiate them - the answers are certainly different enough. I've edited both, trying to draw clearer distinction between them (the original titles were almost identical, and that's probably why this was closed). – Shog9 Jun 16 '09 at 16:25

2 Answers2

9

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.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Afer reading titles (and some content) of more than 1000 massages on stack overflow concerning c++ and exceptions, I found this very good answer for my question. Thanks. – Jan Herrmann Jun 05 '13 at 12:40
4

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

PaV
  • 737
  • 4
  • 7