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

The Archive Base Latest Questions

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

According to Herb Sutter the code below wouldn’t compile. See this site http://www.gotw.ca/gotw/066.htm from

  • 0

According to Herb Sutter the code below wouldn’t compile. See this site http://www.gotw.ca/gotw/066.htm from where I’ve extracted the following text, regarding function-try-blocks :

Toward Some Morals

Incidentally, this also means that the only (repeat
only) possible use for a constructor function-try-block is to
translate an exception thrown from a base or member subobject. That’s
Moral #1. Next, Moral #2 says that destructor function-try-blocks are
entirely usele–

“–But wait!” I hear someone interrupting from the middle of the room.
“I don’t agree with Moral #1. I can think of another possible use for
constructor function-try-blocks, namely to free resources allocated in
the initializer list or in the constructor body!”

Sorry, nope. After all, remember that once you get into your
constructor try-block’s handler, any local variables in the
constructor body are also already out of scope, and you are guaranteed
that no base subobjects or member objects exist any more, period. You
can’t even refer to their names. Either the parts of your object were
never constructed, or those that were constructed have already been
destroyed. So you can’t be cleaning up anything that relies on
referring to a base or member of the class (and anyway, that’s what
the base and member destructors are for, right?).

Assuming this quote, the following code should not compile, as the object cat has already been destructed by the time the process runs into the catch clause. But it does, at least with VSC2008.

class Cat
{
    public:
    Cat() { cout << "Cat()" << endl; }
    ~Cat() { cout << "~Cat()" << endl; }
};

class Dog
{
    public:
    Dog() { cout << "Dog()" << endl; throw 1; }
    ~Dog() { cout << "~Dog()" << endl; }
};


class UseResources
{
    class Cat *cat;
    class Dog dog;

    public:
    UseResources();
    ~UseResources() { delete cat; cat = NULL; cout << "~UseResources()" << endl; }
};

UseResources::UseResources() try : cat(new Cat), dog() { cout << "UseResources()" << endl; } catch(...)
{
    delete cat;
    throw;
}
  • 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-27T07:54:22+00:00Added an answer on May 27, 2026 at 7:54 am

    I don’t think Herb Sutter is actually saying that it won’t compile. He is just explaining the consequences of what the standard has to say about the situation (15.3.10):

    Referring to any non-static member or base class of an object in the
    handler for a function-try-block of a constructor or destructor for
    that object results in undefined behavior.

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

Sidebar

Related Questions

According to this article from Herb Sutter, one should always pick Class Specializing over
According to the site, http://www.dba-oracle.com/t_nls_lang.htm Problem might occur even if both the database and
According to: http://www.datastax.com/docs/1.0/ddl/column_family#about-column-family-compression The reason RDBMSs see a performance degredation as a result of
According to this great article about HTTP uploads by Scott Hanselman, the browser typically
According to http://www.bennadel.com/blog/1892-You-Cannot-Bind-The-Submit-Event-To-Objects-Using-jQuery.htm It is a bug: I am pretty sure this is a
According to author's post in version 2.0 http://lostechies.com/jimmybogard/2011/09/29/automapper-2-0-nestedchild-containers/ but this test doesnt work, can
According to Apple's documentation here , this should work in my code: @interface Menu
According to this discussion , the iphone agreement says that it doesn't allow loading
According to what I have found so far, I can use the following code:
According to the answers to this question, I cannot embed a file version in

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.