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 1058439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:01:41+00:00 2026-05-16T18:01:41+00:00

So, I am try to write a simple base Exception class for C++, based

  • 0

So, I am try to write a simple base Exception class for C++, based on the Java Exception class.
I’m sure there are great libraries out there already, but I am doing this for practice, not production code, and I’m curious and always looking to learn. One of the things the Java’s Exception does, which I would like to also implement, is the concept of a ’cause’. In Java, a new Exception with a cause looks like:

Exception cause = new Exception();
Exception newExcept = new Exception(cause);

However, in C++, passing an Exception as an argument to the constructor is how the copy constructor is called. So, there’s the conceptual disconnect between copying the Exception and creating a new Exception with a cause. This isn’t a problem in Java, obviously.

I guess I’m just wondering what the best way to handle this would be. A few ideas I had were:

  • Differentiate with a dummy variable
  • Just create new Exception, and called setCause() method
  • Something like copy constructor is Exception(Exception &) and constructor with cause is Exception(Exception *)

Thanks

  • 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-16T18:01:42+00:00Added an answer on May 16, 2026 at 6:01 pm

    The exception – when allocated on the stack (I would strongly recomend this) – is freed after the catch clause. So you need to create a copy of the “inner” exception in the newly created exception. If you catch a base class of your exception it will loose it’s correct type unless you give your exception a clone method.

    #include <string>
    #include <exception>
    
    class MyBaseException : public std::exception
    {
    public:
        MyBaseException(const std::string& what = std::string("MyBaseException"))
            : m_BaseException(0), m_What(what) {}  //Constructor without inner exception
    
        MyBaseException(const MyBaseException& innerException, const std::string& what = std::string("MyBaseException"))
            : m_BaseException(innerException.clone()), m_What(what) {}  //Constructor with inner exception
    
        template <class T>  // valid for all subclasses of std::exception
        MyBaseException(const T& innerException, const std::string& what = std::string("MyBaseException"))
            : m_BaseException(new T(innerException)), m_What(what) {}
    
        virtual ~MyBaseException() throw()
            { if(m_BaseException) { delete m_BaseException; } } //don't forget to free the copy of the inner exception
        const std::exception* base_exception() { return m_BaseException; }
        virtual const char* what() const throw()
            { return m_What.c_str(); } //add formated output for your inner exception here
    private:
        const std::exception* m_BaseException;
        const std::string m_What;
        virtual const std::exception* clone() const
            { return new MyBaseException(); } // do what ever is necesary to copy yourselve
    };
    
    int main(int argc, char *argv[])
    {
        try {
            try {
                throw std::exception();
            }
            catch(const std::exception& e) {
                throw MyBaseException(e, "bad");
            }
        }
        catch (const MyBaseException& e) {
            throw MyBaseException(e, "even worse");
        }
        //throw MyBaseException(1, "will not compile");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to write a simple user script to enlarge the picture when you
I try to write DSL class Warcraft class << self def config unless @instance
I try to write :ab in Vim for faster coding but the question is
I try to write a site with CodeIgniter but I've a problem with PHP.
I try to write simple application using OpenMP. Unfortunately I have problem with speedup.
I write simple C++ code that compute array reduction sum, but with OpenMP reduction
hello i got a problem, whenever i try to write any simple thing inside
I try to load image with processing.js using CoffeeScript and write this simple code:
I'm a beginner in Objective-C, and I decided to try to write some simple
Simple answer: The jQuery library my code base was using was out of date.

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.