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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:25:54+00:00 2026-05-27T06:25:54+00:00

In this C++ tutorial , in the section titled Standard exceptions, there is this

  • 0

In this C++ tutorial, in the section titled “Standard exceptions”, there is this example code which uses a class derived from the standard exception class in the STL:

// standard exceptions
#include <iostream>
#include <exception>
using namespace std;

class myexception: public exception
{
  virtual const char* what() const throw()
  {
    return "My exception happened";
  }
} myex; //Declares an instance of myexception outside of the main function

int main () {
  try
  {
    throw myex;
  }
  catch (exception& e) //My question is regarding this line of code
  {
    cout << e.what() << endl;
  }
  return 0;
}

That code prints out My exception happened. However, if I remove the ampersand, it prints out std::exception, which is what happens when you call what() with the standard exception class, not the derived class.

The website gives this explanation:

We have placed a handler that catches exception objects by reference
(notice the ampersand & after the type), therefore this catches also
classes derived from exception, like our myex object of class
myexception.

Is throwing myex kind of like “calling a the catch function, and passing myex as a parameter”? Because it that case, I would imagine that it doesn’t matter whether you throw the exception by value or by reference (that’s what the ampersand does right?), because you are still throwing a myexception rather than an exception. And due to dynamic binding and polymorphism or something like that, e.what() should still print out My exception happened rather than std::exception.

  • 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-27T06:25:54+00:00Added an answer on May 27, 2026 at 6:25 am

    This is happening due to object slicing.

    If you assign an object of a derived class to an instance of a base class (as in the case of using a copy c’tor when passing by value) all the information of the derived class will be lost (sliced).

    For example,

    class Base {
       int baseInfo;
    };
    
    class Derived : public Base
    {
       int someInfo;
    };
    

    Then if you were to write this:

    Derived myDerivedInstance;
    
    Base baseInstance = myDerivedInstance;
    

    Then the “someInfo” in myDerivedInstance is sliced away in baseInstance.

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

Sidebar

Related Questions

I'm following this tutorial on how to extend Python with C\C++ code. The section
This tutorial on mobileorchard.com uses 2 classes (or 2 sets of .h and .m)
I have followed this tutorial which allowed me to create a Silverlight DataGrid that
I'm running through this tutorial found here: http://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htm which teaches how to pass a
I am currently attempting to follow along with section 10.38 from RoR Tutorial. All
Using this tutorial at this section 4.5 : http://www.vogella.de/articles/Android/article.html#first_uiproperties as a guide, I attempt
I´ve already asked this question at the comment section of the tutorial-site http://tympanus.net/codrops/2011/10/12/flexible-slide-to-top-accordion/ I
The HTTP commons client 4.12 tutorial section on exception handling clearly shows that one
Came across this error when trying out the ruby on rails tutorial section with
I'm reading this section of Java EE tutorial. http://download.oracle.com/javaee/6/tutorial/doc/bncfu.html#bncfy And have a question: If

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.