473,699 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RTTI and catch(...)

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

Apr 13 '07 #1
3 3221
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_er ror is derived from std::exception
class unknown_excepti on : public std::runtime_er ror
{
public:
unknown_excepti on() : std::runtime_er ror("unknown exception") { }
};

int main ()
{
try
{
throw unknown_excepti on();
}
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_excepti on
*/

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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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
2043
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 was wondering if it is possible to identify objects during runtime via RTTI because I don't want to be doing: gc_ptr<int>::doGC(); gc_ptr<double>::doGC();
2
5975
by: shishir | last post by:
Please consider the following //file test.cpp #include <iostream> int main() { int x; try { throw x;
6
1941
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 #name; } #define DEF_RTTI(name) inline const char *Name(){ return #name; } And(for example) class CProva
9
4620
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 doesn't sound too bad. What I'm worried about is whether there is a general performance overhead caused by enabling RTTI, such as enabling exceptions makes a C++ program slower even when there are no exceptions actually used. Are there similar...
2
3774
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 needs of my dll? Does it bloat the actual download size - would my dll be smaller without it? thanks - I'm using rtti in some instances, but I jsut want to know if it's costing me hundreds of K in extra download. -denny-
3
1619
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 einfach wie möglich machen, aber nicht einfacher" or "Rube Goldberg"? The first part of this has to do with a technique used in the C++ Standard Library, so I suspect the purist will not have any objection. It's the approach used to build the...
5
3022
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 data types, so I have to implement the RTTI by myself. I need a simple and effective example to help me decide how to design. Can someon help me?
33
3557
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 }; Type GetType() { return type_; }
2
2243
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 integers, then this procedure will be more fast, so I create something like this: --------------------------------------------- class A { public:
0
8618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8916
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8885
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.