473,382 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

RTTI and catch(...)

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

Apr 13 '07 #1
3 3208
lf******@gmail.com wrote:
Is it possible to get the type of a "unknown" exception using RTTI?
Tanks in advance.
Please rephrase that.
Apr 13 '07 #2
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).

Apr 14 '07 #3
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: ja*********@gmail.com
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

Apr 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rick | last post by:
Hi, I wrote a few classes recently and am writing a small GC implementation for C++. I will be implementing my own gc_ptr type maintain a list where I'll store pointers to allocated memory. I...
2
by: shishir | last post by:
Please consider the following //file test.cpp #include <iostream> int main() { int x; try { throw x;
6
by: Kleidemos | last post by:
If I implement a simple RTTI system, more simple than C++ RTTI system for my program and this system is plus or minus: #define DEF_RTTI_BASE(name) virtual inline const char *Name(){ return...
9
by: Agoston Bejo | last post by:
Hello there, I would like to know what overheads there are to think of when using RTTI. I understand that enabling RTTI increases the sizes of the classes, but not the objects themselves. This...
2
by: denny | last post by:
Hey all, I know that dynamic_cast<> takes some time, but , for instance, is there a memoy cost associated in with it? Does it have to maintain a table in memory, thus bloating the runtime ram...
3
by: Steven T. Hatton | last post by:
I'm trying to work out a design for dynamically determining file types, and for generating new files of a given type. I'm curious to know what others think of my current strategy. Is it "so...
5
by: dotNeter | last post by:
I'm studying the RTTI, and my current work is concern for how to get the self-defined type at runtime, that's exactly what the RTTI does. I mean, in my application, I built several self-defined...
33
by: mscava | last post by:
Well I've got a problem, that is more theoretical than practital. I need to know benefits of RTTI. I see another way of doing it... class A { public: ~virtual A() {} enum Type { X, Y, Z }; ...
2
by: Chameleon | last post by:
I know than dynamic_cast check string name of derived to base class and if one of them match, return the pointer of that object or else zero. I suppose, I dynamic_cast instead of strings, checks...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.