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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:38:53+00:00 2026-05-31T23:38:53+00:00

#include <iostream> using namespace std; int main (int argc, char* const argv[]) { int

  • 0
#include <iostream>
using namespace std;

int main (int argc, char* const argv[]) {
    int a = 20;
    cout<<"address of a is "<<&a<<endl;
    try{
        throw a;
    }
    catch (int& z) {
            cout<<"address of z is "<<&z<<endl;
    }
    return 0;
}

The address of a is not same as that of z. It means reference doesn’t work in try catch. If not then why compiler is not generating any error? And what does above code mean?

  • 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-31T23:38:54+00:00Added an answer on May 31, 2026 at 11:38 pm

    I guess my comment was not so clear so I write something as answer.

    When you throw that exception you throw it by value. It means that it’ll be copied (it doesn’t matter if it’s a primitive type or a copied object). Of course when an object is copied then its memory location changes (it’s a new object). Why this? Because exceptions can unroll the stack to find a proper catch block. If you throw something by reference what you may get is garbage (because when a variable goes out of scope it’ll be destroyed). I said may because in reality the compiler does not allow to throw a reference and you’ll always have a copy.

    If your exception object is really big you may consider to allocate the object with new and then throw its pointer. But someone has to deallocate it. Are you ready for the risk? Take a look (as example) at the CException implementation on MFC and its Delete() method (they try to make this easy but I’m not really happy of that).

    Maybe you wonder why if you have to throw by value you catch by reference. First because it’s how std::exception is designed to be used (try to call the what() method of an exception catched by value). Second because it lets the compiler to perform some optimizations (moreover, even if you do not use std::exception, you won’t create useless copies of that object).

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

Sidebar

Related Questions

I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
#include <iostream> using namespace std; int main() { cout << !!!Hello World!!! << endl;
The following code #include <iostream> using namespace std; int main() { const char* const
#include <iostream> using namespace std; int main() { double u = 0; double w
Example: #include <iostream> using namespace std; int main() { wchar_t en[] = LHello; wchar_t
Short problem: #include <iostream> using namespace std; int main() { double **T; long int
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open
#include <iostream> #include <vector> using namespace std; int main(void) { int i, s, g;
Consider the following code: #include <iostream> using namespace std; int main() { int x,

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.