Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > RTTI and catch(...)

Reply
Thread Tools

RTTI and catch(...)

 
 
lfnovaes@gmail.com
Guest
Posts: n/a
 
      04-13-2007
Is it possible to get the type of a "unknown" exception using RTTI?
Tanks in advance.

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      04-13-2007
wrote:
> Is it possible to get the type of a "unknown" exception using RTTI?
> Tanks in advance.
>


Please rephrase that.
 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      04-13-2007
On Apr 13, 6:41 pm, lfnov...@gmail.com 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).

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-14-2007
On Apr 14, 12:41 am, lfnov...@gmail.com 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: RTTI and Multiple Dispatch Nick Keighley C++ 3 08-02-2012 11:31 AM
C++ RTTI and derived classes Shaun C++ 2 12-15-2009 03:28 AM
RTTI and exceptions in Embedded Visual C++ (4.0) EiZi C Programming 0 06-09-2008 04:40 PM
RTTI and CObject Question Bryan C++ 2 07-15-2007 03:28 AM
Types as tags and RTTI for flow control Steven T. Hatton C++ 3 03-06-2006 05:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57