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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:18:20+00:00 2026-06-13T22:18:20+00:00

I started using std::exception as a base class for all my exceptions recently. I

  • 0

I started using std::exception as a base class for all my exceptions recently. I could not properly override what() without putting the virtual keyword in front of it. Without the virtual keyword it always seemed to call the what() function of the base class, std::exception.

It perplexed me a little because I thought that one never needed to put virtual in front of a function when overriding it (and here is a post that seems to confirm that). But I decided to let it go and move on.

Then today while reading O’Reilly’s “Safe C++” I found the author also overriding what() with the virtual keyword. He wrote…

virtual const char* what() const throw () { /* stuff */ }

Why is he overriding a function and using the virtual keyword? Is it just for “documentation” as suggested in the post I quoted above?

  • 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-13T22:18:21+00:00Added an answer on June 13, 2026 at 10:18 pm

    You do not have to put the virtual keyword in front of your override of what() in order to call the subclass implementation. Perhaps when you discovered that it was invoking the base class implementation the exception object you were referencing had been sliced via an inappropriate pass of the exception? For example, I always catch by reference (per Scott Meyers’ recommendation), but if I caught by the exception value and declared the catch as a superclass of the subclass that might be thrown, then the object would be sliced when I caught it. In other words, if I had this exception subclass declared:

    class my_exception : public std::exception
    ...
    

    and I caught an instance of it like so:

    try
    {
        ...
        throw my_exception("Some message");
    }
    catch (std::exception e)
    {
        ...
    }
    

    e in the catch block would be a sliced object. You should catch an exception like this:

    try
    {
        ...
        throw my_exception("Some message");
    }
    catch (std::exception& e)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I could not sleep last night and started thinking about std::swap . Here is
I have recently started using boost::exception. Now I would like to use boost::errinfo_nested_exception to
I started using NetBeans today, and it was all going swimmingly, until I came
I've recently started using boost lambda and thought I'd try and use it in
I've recently started using lambdas an awful lot within threads, and want to make
I have recently started using the -Wall compiler switch in an attempt to improve
I've recently started using new C++11 features like lambda expressions to make my code
I recently started using rvalue references and I've run into a case where I
My quest continues ! I started using a list for my priority queue std::priority_queue<charElement,std::list<charElement>,compareCharElt>
I've an event-driven app that has recently started using SQL Express 2008 (instead of

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.