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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:07:53+00:00 2026-06-05T21:07:53+00:00

I would like to create a hierarchy of exceptions. I used the C++ idiom

  • 0

I would like to create a hierarchy of exceptions. I used the C++ idiom “Polymorphic Exception”.

The difficult bit is that I would like those classes to derive from std::exception – to be able to catch any exception at any point of the code with a try … catch(exception &e).

However, I want to handle exceptions differently whether the exception comes from std::exception or from my user-defined exceptions.

This would suggest to use polymorphism, however I cannot define a virtual function on std::exception.

I also tried with function templates (see code below), but it doesn’t work because the template function called is determined at compile time.

#include <iostream>
#include <string>
using namespace std;

#include <boost\type_traits\is_base_of.hpp>
#include <boost\utility\enable_if.hpp>


class BaseError :public exception {
  public:
    virtual void raise(){throw *this;}
    virtual string msg (){ return "This is the base class"; }
  };

class DerivedError: public BaseError {
  public:
    void raise(){throw *this;}
    string msg (){ return "This is the derived class"; }
  };

template <typename T>
typename boost::disable_if<boost::is_base_of<BaseError, T>>::type
handleException(T &e)
{
    cout << "Handling generic exception" << endl;
    cout << e.what() << endl;
}
template <typename T>
typename boost::enable_if<boost::is_base_of<BaseError, T>>::type
handleException(T &e)
{
    cout << "Handling specific exception" << endl;
    cout << e.msg() << endl;
}


int main () {
  BaseError b;
  handleException(b);
  // prints "Handling specific exception"
  // prints "This is the base class"
  try{
      throw exception("Exception !!!");
  }
  catch (exception &e){
      handleException(e);
      // prints "Handling generic exception"
      // prints "Exception !!!"
  }
  try{
      BaseError b;
      b.raise();
  }
  catch (exception &e){
      handleException(e);
      // prints "Handling generic exception" - I would like the specific behaviour
      // prints "Unknown exception"
  }
  try{
      DerivedError d;
      d.raise();
  }
  catch (exception &e)
  {
      handleException(e);
      // prints "Handling generic exception" - I would like the specific behaviour
      // prints "Unknown exception"
  }
  return 0;
}

Any idea of how to achieve this?

Thanks in advance!

  • 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-06-05T21:07:55+00:00Added an answer on June 5, 2026 at 9:07 pm

    You can rethrow an exception at any point, it will be defined if it happens between a catch. For instance:

    try
    {
        // something that throws an exception
    } catch(std::exception& e) {
        try
        {
            throw; // rethrows the original exception
        } catch(std::runtime_error& e) {
            // handle runtime error
        } catch(std::exception& e) {
            // handle everything else
        }
    }
    

    Now the nested try-catch block could be anywhere, it could be a function or even an object destructor. You can compose a set of objects that handle a particular exception, chain them together and call throw; at the end of the chain. Pseudo code:

    template< typename E, typename Base = void >
    struct exception_handler : Base
    {
        void handle() const
        {
            try
            {
                Base::handle();
            } catch( E& e ) {
                // do something
            }
        }
    };
    
    template< typename E >
    struct exception_handler< E, void >
    {
        void handle() const
        {
            try
            {
                throw;
            } catch( E& e ) {
                // do something
            }
        }
    };
    

    Then the above example can be construed like this:

    try
    {
        // something that throws an exception
    } catch(std::exception& e) {
        exception_handler<
            std::exception
          , exception_handler<
                std::runtime_error
            >
        > handler;
        handler.handle();
    }
    

    The same hierarchy can be created manually, using inheritance and regular classes, where you define each of the exception levels and how to handle them.

    struct runtime_error_handler : base_handler
    {
        void handle() const
        {
            try
            {
                throw;
            } catch( std::runtime_error& e ) {
                // if the original exception was a runtime_error it will be caught here
                // otherwise it will be propagated up the stack to exception_handler
    
                // do something if runtime_error
            }
        }
    };
    
    struct exception_handler : runtime_error_handler
    {
        void handle() const
        {
            try
            {
                runtime_error_handler::handle();
            } catch(std::exception& e) {
                // do something else in the general case
            }
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a c++ type that mimic the build-in type exactly.
I would like to create a class that runs something (a runnable) at regular
I would like to create a triangle plot with organization structure (hierarchy) showing the
I would like to create a construct similar to std::iterator_traits::value_type that can work seamlessly
I would like to know if it is fine to create an exception with
I would like to create a table view in my Eclipse plug-in that has
I would like to create the following hierarchy in a sql db. This is
i would like create a array of structure which have a dynamic array :
I would like to create this shape using just css. I am pretty sure
I would like to create a json object to send as a post array,

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.