Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 150891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:21:45+00:00 2026-05-11T09:21:45+00:00

I am writing a library that I would like to be portable. Thus, it

  • 0

I am writing a library that I would like to be portable. Thus, it should not depend on glibc or Microsoft extensions or anything else that is not in the standard. I have a nice hierarchy of classes derived from std::exception that I use to handle errors in logic and input. Knowing that a particular type of exception was thrown at a particular file and line number is useful, but knowing how the execution got there would be potentially much more valuable, so I have been looking at ways of acquiring the stack trace.

I am aware that this data is available when building against glibc using the functions in execinfo.h (see question 76822) and through the StackWalk interface in Microsoft’s C++ implementation (see question 126450), but I would very much like to avoid anything that is not portable.

I was thinking of implementing this functionality myself in this form:

class myException : public std::exception { public:   ...   void AddCall( std::string s )   { m_vCallStack.push_back( s ); }   std::string ToStr() const   {     std::string l_sRet = '';     ...     l_sRet += 'Call stack:\n';     for( int i = 0; i < m_vCallStack.size(); i++ )       l_sRet += '  ' + m_vCallStack[i] + '\n';     ...     return l_sRet;   } private:   ...   std::vector< std::string > m_vCallStack; };  ret_type some_function( param_1, param_2, param_3 ) {   try   {     ...   }   catch( myException e )   {     e.AddCall( 'some_function( ' + param_1 + ', ' + param_2 + ', ' + param_3 + ' )' );     throw e;   } }  int main( int argc, char * argv[] ) {   try   {     ...   }   catch ( myException e )   {     std::cerr << 'Caught exception: \n' << e.ToStr();     return 1;   }   return 0; } 

Is this a terrible idea? It would mean a lot of work adding try/catch blocks to every function, but I can live with that. It would not work when the cause of the exception is memory corruption or lack of memory, but at that point you are pretty much screwed anyway. It may provide misleading information if some functions in the stack do not catch exceptions, add themselves to the list, and rethrow, but I can at least provide a guarantee that all of my library functions do so. Unlike a ‘real’ stack trace I will not get the line number in calling functions, but at least I would have something.

My primary concern is the possibility that this will cause a slowdown even when no exceptions are actually thrown. Do all of these try/catch blocks require an additional set-up and tear-down on each function invocation, or is somehow handled at compile-time? Or are there other issues I have not considered?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:21:46+00:00Added an answer on May 11, 2026 at 9:21 am

    I think this is a really bad idea.

    Portability is a very worthy goal, but not when it results in a solution that is intrusive, performance-sapping, and an inferior implementation.

    Every platform (Windows/Linux/PS2/iPhone/etc) I’ve worked on has offered a way to walk the stack when an exception occurs and match addresses to function names. Yes, none of these are portable but the reporting framework can be and it usually takes less than a day or two to write a platform-specific version of stack walking code.

    Not only is this less time than it’d take creating/maintaining a cross-platform solution, but the results are far better;

    • No need to modify functions
    • Traps crashes in standard or third party libraries
    • No need for a try/catch in every function (slow and memory intensive)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a little library that should be used by people who don't know
I am writing an imaging library (using the MIT licence) and would like to
Is there a library that would let me write regexp-like queries for lists of
I'm creating a library that includes both Clojure and Java code, and would like
I am writing a library, and would like to be able to fire a
Ok I am writing a library that will be shard between unix and windows.
I am writing a library that will provide a collection of public types to
I am writing a library that creates multiple elements inside a wrapper element, and
So I'm writing a Javascript library that takes x,y coordinates and uses them to
I'm writing a simple Javascript library that makes use of some WebGL code. I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.