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

  • Home
  • SEARCH
  • 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 1014653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:16:56+00:00 2026-05-16T10:16:56+00:00

I just finished work on a C++-program where I’ve implemented my own exceptions (although

  • 0

I just finished work on a C++-program where I’ve implemented my own exceptions (although derived from std::exception). The practice I’ve applied when one exception causes a chain reaction, propagating the error upwards and giving rise to other exceptions, is to concatenate the error message at each appropriate step across the modules (read classes). I.e. the old exception itself is dropped and a new exception is created, but with a longer error message.

This may have worked for my small program, but I wasn’t very satisfied with my approach in the end. For one, line numbers (although not applied at the moment) and file names are not retained except for the last exception; and really that information is of most interest in the first exception.

I figure this could have been handled better by chaining exceptions together; i.e. the old exception is provided in the constructor of the new exception. But how would that be implemented? Does not exceptions die when they go out of scope from the method, thereby preventing one to use exception pointers? And how to copy and store the exception if the exception can be of any derived class?

This ultimately lead me to consider whether chaining exceptions in C++ is such a good idea after all. Perhaps one should just create one exception and then add additional data to that (like I’ve been doing, but probably in a much better manner)?

What is your response to this? Should exceptions caused by another be chained together to retain a sort of “exception trace” — and how should that be implemented? — or should a single exception be used and additional data attached to it — and how should that be done?

  • 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. Editorial Team
    Editorial Team
    2026-05-16T10:16:56+00:00Added an answer on May 16, 2026 at 10:16 am

    It is necessary to copy the data out of an exception object, into a chain, if you want it to outlive the catch block that receives it, aside from rethrow by throw;. (Which includes, for example, if that catch block exits through a throw obj;.)

    This can be done by putting data to be saved on the heap, and implementing swap (move in C++0x) on your private data inside the exception, for example.

    Of course, you need to be careful when using the heap with exceptions… but then again, in most modern OSes, memory overcommitment completely prevents new from ever throwing, for better or for worse. A good memory margin and dropping exceptions from the chain upon complete meltdown should keep it safe.

    struct exception_data { // abstract base class; may contain anything
        virtual ~exception_data() {}
    };
    
    struct chained_exception : std::exception {
        chained_exception( std::string const &s, exception_data *d = NULL )
            : data(d), descr(s) {
            try {
                link = new chained_exception;
                throw;
            } catch ( chained_exception &prev ) {
                swap( *link, prev );
            } // catch std::bad_alloc somehow...
        }
    
        friend void swap( chained_exception &lhs, chained_exception &rhs ) {
            std::swap( lhs.link, rhs.link );
            std::swap( lhs.data, rhs.data );
            swap( lhs.descr, rhs.descr );
        }
    
        virtual char const *what() const throw() { return descr.c_str(); }
    
        virtual ~chained_exception() throw() {
            if ( link && link->link ) delete link; // do not delete terminator
            delete data;
        }
    
        chained_exception *link; // always on heap
        exception_data *data; // always on heap
        std::string descr; // keeps data on heap
    
    private:
        chained_exception() : link(), data() {}
        friend int main();
    };
    
    void f() {
        try {
            throw chained_exception( "humbug!" );
        } catch ( std::exception & ) {
            try {
                throw chained_exception( "bah" );
            } catch ( chained_exception &e ) {
                chained_exception *ep = &e;
                for ( chained_exception *ep = &e; ep->link; ep = ep->link ) {
                    std::cerr << ep->what() << std::endl;
                }
            }
        }
    
        try {
            throw chained_exception( "meh!" );
        } catch ( chained_exception &e ) {
            for ( chained_exception *ep = &e; ep->link; ep = ep->link ) {
                std::cerr << ep->what() << std::endl;
            }
        }
    }
    
    int main() try {
        throw chained_exception(); // create dummy end-of-chain
    } catch( chained_exception & ) {
        // body of main goes here
        f();
    }
    

    output (appropriately grumpy):

    bah
    humbug!
    meh!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just finished the Django tutorial and started work on my own project, however,
I just finished debugging a problem, where our program crashed on a production server,
In other words, how do people work with Git? I just finished uploading my
I just finished working out a memory allocation problem with the current program I'm
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
Just finished up my first mvc4 app. Everything is working great until I deploy
Just finished reading Jon Skeet's article about events and delegates and got a question.
I just finished reading the HTML5 Developer's Cookbook and have a question. I've read
I just finished this script it works well, but I am struggling to pass
I just finished looking at this question: https://stackoverflow.com/questions/753122/which-cloud-computing-platform-should-i-choose But, I am not certain what

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.