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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:26:49+00:00 2026-05-24T12:26:49+00:00

What is the type of the exception object in the following thrown: Question1> range_error

  • 0

What is the type of the exception object in the following thrown:

Question1> range_error r("error"); throw r;

Answer1> an object of range_error

Question2> exception *p = &r; throw *p;

Answer2> a sliced object of exception

Question3> exception *p = &r; throw p;

Answer3> a pointer pointing to range_error is thrown. The capture-handling can access the range_error member functions through dynamic binding.

Do I get these question right?

// Updated and Compiled and Run on VS2010

#include <iostream>
using namespace std;

class ExClassA
{
public:
    virtual void PrintMe() const
    {
        cout << "ExClassA" << endl;
    }
};

class ExClassB : public ExClassA
{
public:
    virtual void PrintMe() const
    {
        cout << "ExClassB" << endl;
    }
};

int main(int argc, char* argv[])
{   
    ExClassB exClassB;
    ExClassA *p = &exClassB;

    try
    {
        throw *p;
    }
    catch (const ExClassA& e)
    {
        e.PrintMe();        
    }

    try
    {
        throw p;
    }
    catch (const ExClassA* e)
    {
        e->PrintMe();
    }
}

The first try-catch of above program prints “ExClassA”

The second try-catch of above program prints “ExClassB”

  • 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-24T12:26:51+00:00Added an answer on May 24, 2026 at 12:26 pm

    I think you are right in all three. The type of the thrown object (IIRC) is the static type of the object being thrown. I would have to dig into the standard for a while to find the exact quotes, but a simple example seems to confirm this:

    struct base {};
    struct derived : base {};
    void t() {
        derived d;
        base * b = &d;
        throw *b;
    }
    int main() {
        try {
            t();
        } catch ( derived const & ) {
            std::cout << "derived" << std::endl;
        } catch ( base const & ) {
            std::cout << "base" << std::endl;
        }
    }
    

    If the dynamic type of the object being thrown was used, then *b would have type derived and the first catch would succeed, but empirically the second catch is executed (g++).

    In the last case, the object thrown is a pointer to exception that refers to a range_error object. The slight difference is again what can be caught, the compiler will not catch in a catch (range_error*) block. The answer is correct, but I would have specified the type of the pointer, as much as the type of the pointee. (The type of the pointer is somehow implicit in the answer)

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

Sidebar

Related Questions

I have following exception in the foreach loop Unable to cast object of type
I'm baffled. My site randomly throws the following error: System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException'
I received the following crash report (truncated) Exception Type: EXC_CRASH (SIGSEGV) Exception Codes: 0x00000000,
The following code throws the aforementioned error message at the line where an object
When I execute my code, the following exception was thrown (German): Ausnahme beim Aufrufen
I have the following scenario, with exception being thrown when I try to cast:
The following throws exception. The course object that is being passed to InsertOnSubmit is
I've created class that takes Exception type in constructor private readonly Exception _exception; public
What is the most appropriate .Net exception type for when you have a class
When using Marshal.GetExceptionCode() how do you get the Exception Type and/or instance of the

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.