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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:20:53+00:00 2026-05-11T01:20:53+00:00

Bjarne Stroustrup writes in his C++ Style and Technique FAQ , emphasis mine: Because

  • 0

Bjarne Stroustrup writes in his C++ Style and Technique FAQ, emphasis mine:

Because C++ supports an alternative that is almost always better: The ‘resource acquisition is initialization’ technique (TC++PL3 section 14.4). The basic idea is to represent a resource by a local object, so that the local object’s destructor will release the resource. That way, the programmer cannot forget to release the resource. For example:

class File_handle {     FILE* p; public:     File_handle(const char* n, const char* a)         { p = fopen(n,a); if (p==0) throw Open_error(errno); }     File_handle(FILE* pp)         { p = pp; if (p==0) throw Open_error(errno); }      ~File_handle() { fclose(p); }      operator FILE*() { return p; }      // ... };  void f(const char* fn) {     File_handle f(fn,'rw'); // open fn for reading and writing     // use file through f } 

In a system, we need a ‘resource handle’ class for each resource. However, we don’t have to have an ‘finally’ clause for each acquisition of a resource. In realistic systems, there are far more resource acquisitions than kinds of resources, so the ‘resource acquisition is initialization’ technique leads to less code than use of a ‘finally’ construct.

Note that Bjarne writes ‘almost always better’ and not ‘always better’. Now for my question: What situation would a finally construct be better than using the alternative construct (RAII) in C++?

  • 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. 2026-05-11T01:20:54+00:00Added an answer on May 11, 2026 at 1:20 am

    The only reason I can think of that a finally block would be "better" is when it takes less code to accomplish the same thing. For example, if you have a resource that, for some reason doesn’t use RAII, you would either need to write a class to wrap the resource and free it in the destructor, or use a finally block (if it existed).

    Compare:

    class RAII_Wrapper {     Resource *resource;  public:     RAII_Wrapper() : resource(aquire_resource()) {}      ~RAII_Wrapper() {         free_resource(resource);         delete resource;     }      Resource *getResource() const {         return resource;     } };  void Process() {     RAII_Resource wrapper;     do_something(wrapper.resource); } 

    versus:

    void Process() {     try {         Resource *resource = aquire_resource();         do_something(resource);     }     finally {         free_resource(resource);         delete resource;     } } 

    Most people (including me) would still argue that the first version is better, because it doesn’t force you to use the try…finally block. You also only need to write the class once, not duplicate the code in every function that uses the resource.

    Edit: Like litb mentioned, you should use an auto_ptr instead of deleting the pointers manually, which would simplify both cases.

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

Sidebar

Related Questions

In The C++ Programming Language, Bjarne writes that the null pointer is not the
In Bjarne Stroustrup's book The C++ Programming Language, it is stated that a derived
Bjarne Stroustrup writes: When defining the functions for a class with a virtual base,
Bjarne Stroustrup (C++ creator) once said that he avoids "do/while" loops, and prefers to
In the Bjarne Stroustrup C++ Book (chapter 13, page 331), it said that a
I read on the wikipedia page for Null_pointer that Bjarne Stroustrup suggested defining NULL
In Bjarne Stroustrup 's home page ( C++11 FAQ ): struct X { int
I'm studying The C++ Programming Language from Bjarne Stroustrup and he talks about logical
I recently watched GoingNative2012 about, of course, C++11. In Bjarne Stroustrup section, I found
According to Bjarne Stroustrup: if (and only if) you use an initialized member 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.