473,626 Members | 3,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to obtain the type of the exception in 'catch (...)'?

Hi.

I need to identify the type of the exception in the universal handler
(catch (...)) for debugging purposes. Point is that I write a testing
console application, which must call some functions from a DLL. One of
these functions throws an exception, and since my small application
knows nothing about the exception type system of that DLL application,
the generated exception always caught in 'catch (...)'. I've made some
small investigations: it turned out (as I could understand...), that
MSVC recognizes the right handler by the name of the exception type.
E.g., I've created a DLL, exporting function 'Func', throwing an
object of type 'A':

*** DLL's implementation part ***

class A
{
public:
int m_a, m_b;
double m_c;
};

__declspec(dlle xport) int Func()
{
throw A();
}

*************** *************** *******

And here's a small .exe, which will call 'Func' from DLL:

*** EXE's implementation part ***

class A
{
};

int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;

try
{
Func();
}
catch (const A&)
{
cerr << "Caught 'A'\n";
}
catch (...)
{
cerr << "Unknown exception...\n" ;
}

return 0;
}

*************** *************** *************** *************

It's surprising, that the program writes out "Caught 'A'", though
conceptually the type of the thrown object is not the same... And this
makes me think, that runtime somehow knows the name of the thrown
type, and moreover, it identifies the needed handler by the NAME of
the type of exception object. So, how to obtain that name in catch
(...)?

Thanks in advance
Martin

Aug 2 '07 #1
3 2514
Martin <ma******@mail. ruwrote in news:1186075859 .605530.315250
@i13g2000prf.go oglegroups.com:
Hi.

I need to identify the type of the exception in the universal handler
(catch (...)) for debugging purposes. Point is that I write a testing
console application, which must call some functions from a DLL. One of
these functions throws an exception, and since my small application
knows nothing about the exception type system of that DLL application,
the generated exception always caught in 'catch (...)'. I've made some
small investigations: it turned out (as I could understand...), that
MSVC recognizes the right handler by the name of the exception type.
E.g., I've created a DLL, exporting function 'Func', throwing an
object of type 'A':
[snip]

Two items:
1) DLLs are off-topic in comp.lang.c++ (please ask over in a Microsoft
newsgroup)
2) You cannot determine the type of a thrown exeception object from within
a catch(...) clause.
Aug 2 '07 #2
On Aug 2, 7:30 pm, Martin <marti...@mail. ruwrote:
I need to identify the type of the exception in the universal handler
(catch (...)) for debugging purposes.
You can't, at present. I believe, however, that support for
this is being added to the standard, and will be present in the
next version of the standard.
Point is that I write a testing
console application, which must call some functions from a DLL. One of
these functions throws an exception, and since my small application
knows nothing about the exception type system of that DLL application,
the generated exception always caught in 'catch (...)'. I've made some
small investigations: it turned out (as I could understand...), that
MSVC recognizes the right handler by the name of the exception type.
E.g., I've created a DLL, exporting function 'Func', throwing an
object of type 'A':
The fact that it's a DLL doesn't make any difference, formally.
(In practice, it might, depending on the implementation. But
there, you're in an implementation dependant situation, and
you'd have to ask in a Windows group.)
*** DLL's implementation part ***
class A
{
public:
int m_a, m_b;
double m_c;
};
__declspec(dlle xport) int Func()
{
throw A();
}
*************** *************** *******
And here's a small .exe, which will call 'Func' from DLL:
*** EXE's implementation part ***
class A
{
};
If you link these two modules together, dynamically or
otherwise, you have undefined behavior, at least according to
the rules of C++. You've violated the one definition rule.

I think that Windows does allow this, as an extention; that the
type in the DLL remains limited to the DLL. But if that's the
case, then you have a different problem: you've lied to the
compiler, because you've said that the type won't escape from
the DLL, and it does, in the form of an exception. Again,
you're dealing with a Windows extension, and will have to ask in
a Windows group, but if I were implementing this, the only way
you could legally refer to an object of the non-exported type
outside of the DLL would be through a void*.
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
try
{
Func();
}
catch (const A&)
{
cerr << "Caught 'A'\n";
}
catch (...)
{
cerr << "Unknown exception...\n" ;
}
return 0;
}
*************** *************** *************** *************
It's surprising, that the program writes out "Caught 'A'", though
conceptually the type of the thrown object is not the same...
Since you have undefined behavior, nothing is surprising:-). In
fact, this behavior doesn't surprise me from a practical point
of view either; C++ uses name equivalence: a type named A is (or
had better be) the same type everywhere.
And this makes me think, that runtime somehow knows the name
of the thrown type, and moreover, it identifies the needed
handler by the NAME of the type of exception object.
The runtime identifies the object by its type. In C++, however,
two types are the same type if and only if they have the same
type name. (Note that typedef doesn't change either the type,
nor its name; it only allows you to refer to it by a different
name.)
So, how
to obtain that name in catch (...)?
For the moment, you can't.

--
James Kanze (GABI Software) email:ja******* **@gmail.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

Aug 3 '07 #3
On 2007-08-03 05:00:45 -0400, James Kanze <ja*********@gm ail.comsaid:
On Aug 2, 7:30 pm, Martin <marti...@mail. ruwrote:
>I need to identify the type of the exception in the universal handler
(catch (...)) for debugging purposes.

You can't, at present. I believe, however, that support for
this is being added to the standard, and will be present in the
next version of the standard.
The main addition to exceptions (voted into the WD two weeks ago in
Toronto) is being able to "save" an exception object via a handle and
rethrow it later. An earlier version of that paper had extensions to
type_info, but those were removed. So there's currently no proposal for
getting the type of an exception object.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 3 '07 #4

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

Similar topics

8
5831
by: Taylor | last post by:
I've run in to code with this pattern: try { // do some potentially bad stuff } catch(System.Exception ex) { throw ex; }
23
3061
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
2
1864
by: steven chong | last post by:
Hi, A button event on my page actually triggers a chain of method calls, and each of these methods has its own try catch statement. The error is thrown all the way from DataAccess->Business->and subsequently handled in the Presentation layer. My problem is, if i do a try catch, the inner exception of the exception which i get in the presentation layer only shows what happened in presentation. The bus, and presentation exceptions...
6
4442
by: bill salkin | last post by:
I setup a "Try..." block and attempted to open a non- existant database. It went to the second "catch" not the first. What is the proper "catch" clause for this specific case? TIA, Bill
6
5173
by: Charles Law | last post by:
I want to do something like this: obj = CType(value, Value.Type) Well, not exactly, but I think that captures the essence. I realise it won't work as I have written it, and it looks a bit like a nebulous statement, but I am looking for a generic way to convert a variable of unknown type to its actual type. Perhaps a better example would be
4
1431
by: Arne | last post by:
I have a generic catch block for exceptions of type Exception Once inside the block I would like to know if my exception is of type System.Data.SqlClient.SqlException. Actually I would like to pass the variable of type Exception to a generic error handler, where I need to know the specific Exception. I need to find a something like 'Is Type' or something like that.
4
1232
by: Chris Allen | last post by:
I am confused on one aspect of exception handling. If you specify the exception object type to match in an except statement it is possible to also obtain the exception object itself, but I can't figure out how to get the exception object when I don't specify a match. for example: this works and I can do what I like with the exception object (msg). but I can't do this with a simple except statment.
3
3464
by: Miro | last post by:
I cant seem to find an example on how to do something, ( vb2005.express ) i have a Try ListeningSerialPort.Open() TestText.Enabled = True Catch ex As Exception 'Debug.WriteLine(ex.Message) End Try
10
2045
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
0
8266
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8365
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
8505
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
6125
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
5574
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();...
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
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.