[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

Stack printing in C++

Hi,
I am using exception mechanism as my error handler , but the exception doesnt specify the location of the error , or all the herarchy to the throwing method..so i am thinking to print the stack in addition of printing the exception message whenever an exception is thrown.
is this possible ? ( debuggers do it.)
0
shadis
Asked:
shadis
  • 2
1 Solution
 
jhanceCommented:
What OS are you talking about?
0
 
shadisAuthor Commented:
HP UX 10.20
Linux
Win -NT

My first priority is NT and linux
0
 
shadisAuthor Commented:
sorry , i ment HP and NT
0
 
nietodCommented:
I would have to say you are trying to use exceptions incorrectly.  The idea behind exceptions is that they are detected at a points where they can't behandled, but presumabely there is a point, farther down the call stack, where they can be handled.  So they point should catch the exception and fix the problem.  That point should not care where the problem was, it should just fix the problem.

That aside, it can (sort of) be done.  You can have the exception objects you throw store the source code file name and line number of the exception location.  These can be suplied to the object's constructor using the __FILE__ and __LINE__ pre-processor synbols.

Althernately you can have the object store the name of the procedure that perfrms the exception.

How that gives you the location of the exception, but not the entire call stack.  You can get that too, but that is a little harder.  You can have define a base class exception object that stores an STL vector to store the call stack information.  (procedure name, and or line number etc.)   Then at the end of each procedure place code that catches every exception (every exception derived from this base class) and have the code add the current procedure's information to the call stack, then rethrow the object with the new information.   You can use some C pre-processor macros to automate this.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now