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

The Archive Base Latest Questions

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

When I write my try/catch blocks I always throw objects by value and catch

  • 0

When I write my try/catch blocks I always throw objects by value and catch by reference. I also apply the rule that the most derived classes must come first.

Today I tried to catch BY VALUE violating this main rule. The code below executes without any problem and prints "A4".

Question: what type of CCA argument is passed to the exception handler that executes? The original object thrown is CCB passed by value to a CCA object: is this an example of memory slicing problem or somekind of corruption i.e. the object CCA cannot be totally trusted?

Regards

AFG

  class CCA{
        int m_value;
  public:
        CCA( int value ) : m_value( value ){}            
  };

  class CCB : public CCA{
  public: 
        CCB( int value ): CCA( value ){}
  };


  main(){     
        try{
              throw CCB(4);
        }catch( CCA a ){
              std::cout << " A:" << a.value() << std::endl; 
              // this is the catch clause that executes
        }catch( CCB b ){
           std::cout << " B:" << b.value() << std::endl;
        }     
 }
  • 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:49:15+00:00Added an answer on May 27, 2026 at 6:49 am

    This is slicing: The inability of the base copy constructor to discriminate an argument of the genuinely same type from one of a derived class type:

    struct Base {
      Base(Base const &);  // must accept any derived class
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a delegate function that can 'wrap' some while/try/catch code around
In Java, we handle exceptions using try catch blocks. I know that I can
I try to write KSH script for processing a file consisting of name-value pairs,
I try to write a macro in clojure that sets up a namespace and
I've got (an admittedly strange) try/catch block that checks if a File exists. If
I try to write to a large file, but it seems like it does
I try to write a simple client/server application (all application is a bluetooth service
I try to write a simple Markdown parser in JavaScript. Therefore I want to
I try to write a simple user script to enlarge the picture when you
I try to write the following in latex: \begin{itemize} \item \textbf{insert(element|text)} inserts the element

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.