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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:01:35+00:00 2026-05-22T02:01:35+00:00

I was recently tasked with hunting down a memory leak in a part of

  • 0

I was recently tasked with hunting down a memory leak in a part of our code. The leak ended up being in the destructor for a particular object…and I found something really strange. A former coworker wrote this:

File::~File()
try
{
    Clear();
}
catch (...)
{
    Log("caught exception");
}

The file class inherits from some base classes. My first question is: is this strictly legal C++? It compiles in Visual Studio 2008, but I showed it to a few friends / coworkers and they were fairly horrified that it worked.

It doesn’t actually work as intended, though: the base class that this object inherits from has a destructor that is now never called (as opposed to if you just wrapped the destructor in a regular method block, having the try / catch as part of that method).

Can anyone take a stab at explaining why this is allowed, and why the base class destructor was not called? The destructor here was not throwing.

  • 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-22T02:01:35+00:00Added an answer on May 22, 2026 at 2:01 am

    This is a function try block and it’s completely legal.

    See, for example, here.

    The only time that you can do something in a function try block that you can’t do in a normal try block in a function is catch exceptions thrown by expression in a constructor initializer list (and even then you end up having to throw something), but that doesn’t apply here.

    This GOTW #66 is particularly interesting, although it concentrates more on constructors. It contains this “moral”:

    Since destructors should never emit an exception, destructor function-try-blocks have no practical use at all.

    Just to add clarification, the code as written will cause any exception caught to be rethrown due to ISO/IEC 14882:2003 15.3 [except.handle] / 16:

    The exception being handled is rethrown if control reaches the end of a handler of the function-try-block of a constructor or destructor. […]

    However it is legal to have a parameterless return in the handler of a function try block for a destructor – it is only forbidden in a function try block for a constructor – and this will supress the rethrow of the exception. So either of these alternatives would prevent the exception from leaving the destructor.

    File::~File()
    try
    {
        Clear();
    }
    catch (...)
    {
        Log("caught exception");
        return;
    }
    

    File::~File()
    {
        try
        {
            Clear();
        }
        catch (...)
        {
            Log("caught exception");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our server recently has been going down a lot and I was tasked to
Recently a new co-op was hired at our company and has been tasked to
We use MOSS 2007 (SharePoint) for our intranet. Recently we were tasked with supporting
I have recently been tasked to working on code that uses Java CUP. Does
Recently I found some simple source code of a bootloader.The following is the simple
I was recently tasked with improving some embedded code where a disproportionate amount of
Recently one of our app servers went down, when it was rebooted the Python
I've recently been tasked with porting a large codebase of C++ code to XNA,
I don't have much experience with Serial I/O, but have recently been tasked with
Recently I was tasked with writing up formal procedures for a team based development

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.