Cla08: do not escape exceptions from destructor

Source: Internet
Author: User

Class DBConnection {public:... static DBConnection create (); // This function returns the DBConnection object void close (); // closes the connection. If the connection fails, an exception is thrown };

A better policy is to create a DBConn class used to manage DBConnection resources. The DBConn class provides a close function, which gives the customer a chance to handle "exceptions caused by this operation ". DBConn can also track whether the managed DBConnection has been closed. If it has not been closed, it is disabled by the DBConn destructor. This prevents lost data connections. However, if the DBConnection destructor fails to call close, you can use the "Force end program" or "Swallow exception" method:

Class DBConn {public: DBConn ();~ DBConn (); void close (); private: DBConnection db; bool closed;}; DBConn: DBConn () {} DBConn ::~ DBConn () {if (! Closed) {try {db. close (); // close the connection} catch (...) // If the close action fails {write log, write down the call to close failed; // record and end the program... // or swallow an exception;} void DBConn: close () // The new function {db. close (); closed = true ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.