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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:11:32+00:00 2026-05-27T16:11:32+00:00

I found an example on this link I knew C++ does not support finally

  • 0

I found an example on this link

I knew C++ does not support finally block, so if any exception thrown during retrieving data (ex: in while block), then Connection, Statement, and Result objects can not be released.

So, I wonder how (or when) these objects released… Or there is another way writing the code to release them?

P/S: I spent much time in other language such as Java, C#, therefore maybe my thought goes wrong in something. Correct me if anything wrong.

  • 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-27T16:11:33+00:00Added an answer on May 27, 2026 at 4:11 pm

    C++ doesn’t have finally blocks because it doesn’t need them. It has something much, much better: destructors.

    A class type object can have a destructor, which will be called for an instance of that class type object when the instance ceases to exist. Local variables, which have what is called “automatic storage duration,” cease to exist at the end of the block in which they are declared.

    Destructors therefore should be used to manage resources. Rather than writing a delete expression at the end of a block to destroy a dynamically allocated object, you should use a smart pointer to manage the lifetime of the object. For example, consider the following lines from the example program to which you link:

    {
        sql::Connection* con;
        // ...
        con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
        // ...
        delete con;
    }
    

    This could instead be written as:

    {
        // ...
        std::unique_ptr<sql::Connection> con(
            driver->connect("tcp://127.0.0.1:3306", "root", "root"));
        // ...
    }   // con is automatically deleted here, even if an exception is thrown.
    

    (If you are using an older compiler or C++ Standard Library implementation and you don’t have or can’t use std::unique_ptr, you can use std::auto_ptr for this specific use case.)

    Note that here the lifetime of the connection object is managed automatically: you don’t have to remember to destroy the object and you don’t have to worry about performing special cleanup if an exception is thrown. The std::unique_ptr destructor will ensure that the connection object is destroyed.

    This technique of using destructors to perform resource cleanup is called Resource Acquisition Is Initialization (RAII) and is the most important idiom to understand and to use consistently in C++. It’s hard to manage resources correctly yourself; it’s a whole lot easier when you let the } do all the hard work for you.

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

Sidebar

Related Questions

I found this link and I wanted to try the carousel example out. I
I found this example and used it How do you resize a Bitmap under
I found this example in the App Engine documentation . 1 <% 2 UserService
i'm using this example implementation found at http://tangentsoft.net/wskfaq/examples/basics/select-server.html This is doing most of what
I found this command line search and replace example: find . -type f -print0
So I found this: http://tiles.apache.org/framework/tutorial/advanced/nesting-extending.html Here is the example: <definition name=myapp.homepage template=/layouts/classic.jsp> <put-attribute name=title
I've searched for an answer and found some c#-examples, but could not get this
I've been using the coding from an example from this link : The code
I have found this example Lazy load of images in ListView from Fedor which
I have found some info on the subject ( like this link) , but

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.