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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:17:23+00:00 2026-05-13T01:17:23+00:00

I am designing an exception hierarchy in C++ for my library. The hierarchy is

  • 0

I am designing an exception hierarchy in C++ for my library. The “hierarchy” is 4 classes derived from std::runtime_error. I would like to avoid the slicing problem for the exception classes so made the copy constructors protected. But apparently gcc requires to call the copy constructor when throwing instances of them, so complains about the protected copy constructors. Visual C++ 8.0 compiles the same code fine. Are there any portable way to defuse the slicing problem for exception classes? Does the standard say anything about whether an implementation could/should require copy constructor of a class which is to be thrown?

  • 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-13T01:17:23+00:00Added an answer on May 13, 2026 at 1:17 am

    I would steer clear of designing an exception hierarchy distinct to your library. Use the std::exception hierarchy as much as possible and always derive your exceptions from something within that hierarchy. You might want to read the exceptions portion of Marshall Cline’s C++ FAQ – read FAQ 17.6, 17.9, 17.10, and 17.12 in particular.

    As for "forcing users to catch by reference", I don’t know of a good way of doing it. The only way that I have come up with in an hour or so of playing (it is Sunday afternoon) is based on polymorphic throwing:

    class foo_exception {
    public:
        explicit foo_exception(std::string msg_): m_msg(msg_) {}
        virtual ~foo_exception() {}
        virtual void raise() { throw *this; }
        virtual std::string const& msg() const { return m_msg; }
    protected:
        foo_exception(foo_exception const& other): m_msg(other.m_msg) {}
    private:
        std::string m_msg;
    };
    
    class bar_exception: public foo_exception {
    public:
        explicit bar_exception(std::string msg_):
            foo_exception(msg_), m_error_number(errno) {}
        virtual void raise() { throw *this; }
        int error_number() const { return m_error_number; }
    protected:
        bar_exception(bar_exception const& other):
            foo_exception(other), m_error_number(other.m_error_number) {}
    private:
        int m_error_number;
    };
    

    The idea is to make the copy constructor protected and force users to call Class(args).raise() instead of throw Class(args). This lets you throw a polymorphicly bound exception that your users can only catch by reference. Any attempt to catch by value should be greeted with a nice compiler warning. Something like:

    foo.cpp:59: error: ‘bar_exception::bar_exception(const bar_exception&)’ is protected

    foo.cpp:103: error: within this context

    Of course this all comes at a price since you can no longer use throw explicitly or you will be greeted with a similar compiler warning:

    foo.cpp: In function ‘void h()’:

    foo.cpp:31: error: ‘foo_exception::foo_exception(const foo_exception&)’ is protected

    foo.cpp:93: error: within this context

    foo.cpp:31: error: ‘foo_exception::foo_exception(const foo_exception&)’ is protected

    foo.cpp:93: error: within this context

    Overall, I would rely on coding standards and documentation stating the you should always catch by reference. Make sure that your library catches exceptions that it handles by reference and throw fresh objects (e.g., throw Class(constructorArgs) or throw;). I would expect other C++ programmers to have the same knowledge – but add a note to any documentation just to be sure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while designing my user control, i encountered the following problem: i would like to
My question is: how would you create exception hierarchy in your application? Designing the
I am designing a loosely-coupled structure. I want to call classes from different assemblies/namespaces
When designing a C API for configuring a library/utility, I have a co-worker who
When designing a stock management database system for sales and purchases what would be
Im designing my school's website and I kind of want to do like a
Possible Duplicate: Should I derive custom exceptions from Exception or ApplicationException in .NET? I've
Or, "how do Russians throw exceptions?" The definition of std::exception is: namespace std {
When designing a class hierarchy, sometimes the subclass has added a new initWithSomeNewParam method,
Why does std::stack::pop() not throw an exception if the stack is empty and there

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.