RTTI and catch(...)

Discussion in 'C++' started by lfnovaes@gmail.com, Apr 13, 2007.

  1. Guest

    Is it possible to get the type of a "unknown" exception using RTTI?
    Tanks in advance.
    , Apr 13, 2007
    #1
    1. Advertising

  2. wrote:
    > Is it possible to get the type of a "unknown" exception using RTTI?
    > Tanks in advance.
    >


    Please rephrase that.
    Gianni Mariani, Apr 13, 2007
    #2
    1. Advertising

  3. Salt_Peter Guest

    On Apr 13, 6:41 pm, wrote:
    > Is it possible to get the type of a "unknown" exception using RTTI?
    > Tanks in advance.


    Not neccessarily, depends what you know about the thrown exception and
    how you are capturing it.
    If the unknown exception is derived from std::exception (or some other
    base), then yes you could get its type since you'ld be catching the
    exception using a base reference.

    ie:

    #include <iostream>
    #include <typeinfo>
    #include <stdexcept>

    // std::runtime_error is derived from std::exception
    class unknown_exception : public std::runtime_error
    {
    public:
    unknown_exception() : std::runtime_error("unknown exception") { }
    };

    int main ()
    {
    try
    {
    throw unknown_exception();
    }
    catch( const std::exception& r_e )
    {
    std::cout << "std exception caught: ";
    std::cout << r_e.what() << std::endl;
    std::cout << "typename: ";
    std::cout << typeid( r_e ).name() << std::endl;
    }
    catch(...) // catch-all
    {
    std:cout << "unexpected exception caught!\n:;
    }
    }

    /*
    std exception caught: unknown exception
    typename: unknown_exception
    */

    What might be thrown could me anything: an integer, an instance of a
    user-type, a string, a long, or hopefully a derivative of
    std::exception.

    The goal is not to catch the exception in the above catch-all block
    (you might have a try-catch block to isolate a problematic area at
    which point you can rethrow a meaningfull exception).
    Salt_Peter, Apr 14, 2007
    #3
  4. James Kanze Guest

    On Apr 14, 12:41 am, wrote:
    > Is it possible to get the type of a "unknown" exception using RTTI?


    Not at present. I think that some sort of support for this is
    being considered for future inclusion. (The compiler/run-time
    obviously has the information, and it probably needed for things
    like join.)

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Apr 14, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Black
    Replies:
    8
    Views:
    4,140
    Xenos
    Aug 20, 2004
  2. Adam
    Replies:
    9
    Views:
    553
    red floyd
    Feb 2, 2006
  3. Steven T. Hatton

    Types as tags and RTTI for flow control

    Steven T. Hatton, Mar 2, 2006, in forum: C++
    Replies:
    3
    Views:
    298
    Steven T. Hatton
    Mar 6, 2006
  4. Bryan

    RTTI and CObject Question

    Bryan, Jul 15, 2007, in forum: C++
    Replies:
    2
    Views:
    404
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    549
    Daniel Pitts
    Aug 5, 2009
Loading...

Share This Page