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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:20:36+00:00 2026-06-02T23:20:36+00:00

You could have a try. The program below compiles and runs smoothly. The address

  • 0

You could have a try. The program below compiles and runs smoothly. The address of variable ex in the constructor is different from that of e, a temporary variable in the catch block. Yet you may notice that the value of ex in line B is passed to e by reference. Could anyone explain what’s going on?

#include<cstring>
#include<iostream>


using std::string;
using std::endl;
using std::cout;

class ThrowException;
ThrowException* TE_ptr;

class ThrowException{

    private:
        string msg;
        int b;
    public:
        ThrowException(string m="Unknown exception",int factor=0) throw(string,const char*);
        ~ThrowException(){        cout<<"destructor get called."<<endl;}
        friend std::ostream& operator<<(std::ostream& os,const ThrowException&TE);
};

ThrowException::ThrowException(string m, int f) throw(string,const char*):msg(m),b(f){
    cout<<"msg="<<msg<<'\n'<<"b="<<b<<endl;
    TE_ptr=this;
    if(b==1){
        string ex("b=1 not allowed.");
        cout<<"The address of e in constructor is "<<&ex<<endl;      //A
        throw ex;
    }
}

std::ostream&operator<<(std::ostream&os, const ThrowException &TE){
    os<<TE.msg<<'\n'<<TE.b<<endl;
}
int main(){

    try{
        ThrowException a("There's nothing wrong.", 1);
    }catch(string &e){             //B
        cout<<"The address of e in first catch block is "<<&e<<endl;        //C
        cout<<"The content resided in the momery block pointed to by TE_ptr is "<<*TE_ptr<<endl;
    }

}

Another problem I would like to ask is when will the destructor of ThrowException object a be called?

  • 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-02T23:20:37+00:00Added an answer on June 2, 2026 at 11:20 pm

    The throw expression copies the thrown object to a safe place before leaving the local scope. The language doesn’t say exactly where it is stored, just that this must work somehow (details left to the implementation).

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

Sidebar

Related Questions

I have a strange problem that I could not solve. When I try to
On Mac OSX 5.8 I have a Java program that runs at 100% CPU
I have noticed that sizzeof() on my program structure is different on x86 and
In T-SQL you could have a query like: SELECT * FROM Users WHERE User_Rights
Could you please have a look at my code below. #!C:\Perl\bin\perl.exe use strict; use
I have a small bit of code that runs in an applet that contains
Hey, This is just a simple exercise from class that I decided to have
In MySql I could have a table with an auto increment column and insert
It looks like a problem which could have simple solution, but I haven't found
I'm new to ASP.NET MVC so this could have an obvious answer. Right now

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.