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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:00:21+00:00 2026-05-10T16:00:21+00:00

Most people say never throw an exception out of a destructor – doing so

  • 0

Most people say never throw an exception out of a destructor – doing so results in undefined behavior. Stroustrup makes the point that ‘the vector destructor explicitly invokes the destructor for every element. This implies that if an element destructor throws, the vector destruction fails… There is really no good way to protect against exceptions thrown from destructors, so the library makes no guarantees if an element destructor throws’ (from Appendix E3.2).

This article seems to say otherwise – that throwing destructors are more or less okay.

So my question is this – if throwing from a destructor results in undefined behavior, how do you handle errors that occur during a destructor?

If an error occurs during a cleanup operation, do you just ignore it? If it is an error that can potentially be handled up the stack but not right in the destructor, doesn’t it make sense to throw an exception out of the destructor?

Obviously these kinds of errors are rare, but possible.

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

    Throwing an exception out of a destructor is dangerous.
    If another exception is already propagating the application will terminate.
    But said "terminate" is a very well specified behaviour of majority of compilers, hence it’s almost never "Undefined Behaviour" (depending on compiler).

    #include <iostream>  class Bad {     public:         // Added the noexcept(false) so the code keeps its original meaning.         // Post C++11 destructors are by default `noexcept(true)` and         // this will (by default) call terminate if an exception is         // escapes the destructor.         //         // But this example is designed to show that terminate is called         // if two exceptions are propagating at the same time.         ~Bad() noexcept(false)         {             throw 1;         } }; class Bad2 {     public:         ~Bad2()         {             throw 1;         } };   int main(int argc, char* argv[]) {     try     {         Bad   bad;     }     catch(...)     {         std::cout << "Print This\n";     }      try     {         if (argc > 3)         {             Bad   bad; // This destructor will throw an exception that escapes (see above)             throw 2;   // But having two exceptions propagating at the                        // same time causes terminate to be called.         }         else         {             Bad2  bad; // The exception in this destructor will                        // cause terminate to be called.         }     }     catch(...)     {         std::cout << "Never print this\n";     }  } 

    This basically boils down to:

    Anything dangerous (i.e. that could throw an exception) should be done via public methods (not necessarily directly). The user of your class can then potentially handle these situations by using the public methods and catching any potential exceptions.

    The destructor will then finish off the object by calling these methods (if the user did not do so explicitly), but any exceptions throw are caught and dropped (after attempting to fix the problem).

    So in effect you pass the responsibility onto the user. If the user is in a position to correct exceptions they will manually call the appropriate functions and processes any errors. If the user of the object is not worried (as the object will be destroyed) then the destructor is left to take care of business.

    An example:

    std::fstream

    The close() method can potentially throw an exception. The destructor calls close() if the file has been opened but makes sure that any exceptions do not propagate out of the destructor.

    So if the user of a file object wants to do special handling for problems associated to closing the file they will manually call close() and handle any exceptions. If on the other hand they do not care then the destructor will be left to handle the situation.

    Scott Meyers has an excellent article about the subject in his book "Effective C++"

    Edit:

    Apparently also in "More Effective C++"
    Item 11: Prevent exceptions from leaving destructors

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

Sidebar

Related Questions

I know that most people say, OH! It's not possible, use php or whatever...
I know that most everything is an object in JavaScript. When people say object
I want to save pictures(mostly JPEG) to MySQL database. I saw most people say
Why do most people say that data services and the database are the most
I know using goto is something most people say to avoid, however I have
Most people talk about progressive enhancement right now as serving browsers with javascript (enhanced
Most people would agree that internationalizing an existing app is more expensive than developing
Most people have heard of Content Management Systems. However, what basic features should an
Do most people use .NET's SqlMembershipProvider, SqlRoleProvider, and SqlProfileProvider when developing a site with
I know that most people have DirectX9-capable cards but is it really necessary to

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.