Is there any way to know from where the C++ exception originates ?

Bogdan Vacaliuc bvacaliuc at ngit.com
Tue Sep 21 22:27:27 UTC 2004


Hello Valery, Angelo:

The topic of 'finding the line of code' that causes an exception has been interesting to me over the course of time.  So I got a wee
bit carried away with my response on this one, but I hope you will humor me.

Ultimately, some form of 'stack backtrace' is implemented to find the address of the instruction that caused the original exception
(usually a hardware trap, etc.).  This is also true for (structured) exception handling (software detected faults) in much the same
way.

Unfortunately, there is little standards-mandated support for such items as the code is inherently non-portable.  The Java language
has built-into its exception system the concept of a stack backtrace which is available in the exception handler to great
popularity.  The same functionality is not widely available in C/C++ implementations.

This does not mean that it is not possible to obtain, it just means that you have to implement something specific for the
arch/platform/compiler that you are using.

---

Below are some links I saved while making a cursory survey of the topic.

A good start is here:
http://www.cplusplus.com/doc/tutorial/tut5-3.html

An interesting treatise on Java exceptions is here:
http://homie.dijas.com/blog/2003/02/02/java-exceptions-dont-have-to-be-useless.html

For the entire google search:
Google: +stack +trace +exception +"c++"

A recent discussion on GCC regarding stack trace generation (vis-a-vis gcj) is here:
http://gcc.gnu.org/ml/java/2004-07/msg00171.html

An interesting (although anecdotal) chapter in the GNAT docs:
http://gcc.gnu.org/onlinedocs/gnat_ugn_unw/Exception-Handling-Control.html

Here is a discussion on comp.std.c++ in Feb'03.  I tie-in at a point that illustrates some of the reasons why the standards
committes *avoid* putting this functionality in:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=d6651fb6.0302040206.1691ef2a%40posting.google.com

Finally, one of the more enlightened posts I have seen on this topic is:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=wbur8zmdnew.fsf%40kaarne.cs.tut.fi
(actually the whole thread is pretty good reading)


Hope this helps,

-bogdan


On Tuesday, September 21, 2004 3:53 PM, Angelo Fraietta wrote:

> Is this a history question or where in the code the exception occurs?
> 
> Valery Pykhtin wrote:
> 
>> Hi All,
>> 
>> may be this is an unappropriate place to ask, but may be someone
>> knows the answer in the topic ? 
>> 
>> Best regards,
>> Valery




More information about the users mailing list