Exception stack trace

M

Markus Dehmann

What's the best way to implement an exception stack trace? What I want
is sth like printStackTrace() in Java. It could look like this:

Car.cpp:381:runtime_exception:Could not find the brake
Police.cpp:123:traffic_observation_exception:Speed too high
Driver.cpp:2131:existential_exception:I am in jail

for the following fictitious program:

int main(){
try{
Driver me;
me.driveHome(); // starts a chain of exceptions
// of which the last one comes up here
me.watchTV();
}catch(exception& e){
cerr << e.what();
}
}

If we don't have the hierarchy, all the client sees is the last
exception, sth like "I am in jail", but that's not enough! He doesn't
know what's going on, so he needs to see the whole chain of reasons.

Thanks!
Markus
 
P

Pete Becker

Markus said:
What's the best way to implement an exception stack trace? What I want
is sth like printStackTrace() in Java. It could look like this:

Unlike Java, C++ doesn't incorporate a debugger into every application.
If you need one you have to provide it. Your compiler typically has one.
Just start it up and run your application.
 
D

Denis Remezov

Markus said:
What's the best way to implement an exception stack trace? What I want
is sth like printStackTrace() in Java. It could look like this:

Car.cpp:381:runtime_exception:Could not find the brake
Police.cpp:123:traffic_observation_exception:Speed too high
Driver.cpp:2131:existential_exception:I am in jail

for the following fictitious program:

int main(){
try{
Driver me;
me.driveHome(); // starts a chain of exceptions
// of which the last one comes up here
me.watchTV();
}catch(exception& e){
cerr << e.what();
}
}

If we don't have the hierarchy, all the client sees is the last
exception, sth like "I am in jail", but that's not enough! He doesn't
know what's going on, so he needs to see the whole chain of reasons.

Perhaps you could make use of uncaught_exception(), which returns true
during stack unwinding. This must have been thought through and
perfected before, but here is a raw idea offhand:

#include <stdio.h>
#include <exception>

class ExceptionWatch {
int line_;
char const* pfname_;
public:
ExceptionWatch(int line, char const* pfname) : line_(line),
pfname_(pfname) {}
~ExceptionWatch() {
if(std::uncaught_exception()) {
//on purpose
printf("line:\t%d\tfile name:\t%s\n", line_, pfname_);
}
}
};

#define EXCEPTWATCH ExceptionWatch exception_watch(__LINE__, __FILE__);

Insert EXCEPTWATCH at the beginning of each function you want to trace:

void func() {
EXCEPTWATCH
//...
}

Denis
 
M

Markus Dehmann

Unlike Java, C++ doesn't incorporate a debugger into every application.
If you need one you have to provide it.

I know that C++ doesn't incorporate it automatically. My question was:
What's the best way to implement such a stack trace (given that
C++ doesn't provide it)...

Is it reasonable to add a list of messages to my exception class? On each
catch, I would add a message (with __FILE, __LINE__ information) to the
caught exception and throw it again, up to the highest level in main().

Markus
 
P

Pete Becker

Markus said:
Is it reasonable to add a list of messages to my exception class? On each
catch, I would add a message (with __FILE, __LINE__ information) to the
caught exception and throw it again, up to the highest level in main().

Seems like a lot of work. What problem are you trying to solve?
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Markus said:
I know that C++ doesn't incorporate it automatically. My question was:
What's the best way to implement such a stack trace (given that C++
doesn't provide it)...
[snip]
If you happen to be developing on a system like Linux, then you could
use one of many utilities such as electric fence. These are designed to
provide the platform specific capabilities you seem to be looking for, &
unless I'm mistaken, they tie into GDB.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFA61HPoo/Prlj9GScRAtovAJ0cXxRAN0rjwIxdBH1aXox4/DtqQACeMwjy
PYGqYyeb+vm6KTalitOk+Xo=
=W3px
-----END PGP SIGNATURE-----
 
M

Markus Dehmann

Evan Carew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Markus said:
I know that C++ doesn't incorporate it automatically. My question was:
What's the best way to implement such a stack trace (given that C++
doesn't provide it)...
[snip]
If you happen to be developing on a system like Linux, then you could
use one of many utilities such as electric fence. These are designed to
provide the platform specific capabilities you seem to be looking for, &
unless I'm mistaken, they tie into GDB.

Well, I want this stack trace as a final output for the end user. I am
writing a kind of compiler where I process user input. I have a parser
that reads user input and constructs sth called Items or int
primitives. When an expression of the user input cannot be parsed, an
exception is thrown, but also an "UnknownItem" is recognized (derived
from Item), and the system tries to move on with this Item as long as
possible.

So, If sth could not be recognized and an "UnknownItem" is propagated,
later functions might be able to process that or again throw an
exception, e.g. that they ecpected an int, but got an Item (namley,
the UnknownItem). So, there are different stages of errors that the
user all wants to see.

Markus
 
P

Pete Becker

Markus said:
Well, I want this stack trace as a final output for the end user. I am
writing a kind of compiler where I process user input. I have a parser
that reads user input and constructs sth called Items or int
primitives. When an expression of the user input cannot be parsed, an
exception is thrown, but also an "UnknownItem" is recognized (derived
from Item), and the system tries to move on with this Item as long as
possible.

This doesn't sound like an appropriate use of exceptions. A stack trace
is an implementation detail: if you restructure the application, the
same mistake will produce a different error message. What you need to do
is tell the user what mistake they made, not what you were doing when
you noticed it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top