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

  • Home
  • SEARCH
  • 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 6880689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:00:23+00:00 2026-05-27T05:00:23+00:00

Possible Duplicate: When is a function try block useful? Difference between try-catch syntax for

  • 0

Possible Duplicate:
When is a function try block useful?
Difference between try-catch syntax for function

This code throws an int exception while constructing the Dog object inside class UseResources. The int exception is caught by a normal try-catch block and the code outputs :

Cat()  
Dog()  
~Cat()  
Inside handler

#include <iostream>
using namespace std;

class Cat
{
    public:
    Cat() { cout << "Cat()" << endl; }
    ~Cat() { cout << "~Cat()" << endl; }
};

class Dog
{
    public:
    Dog() { cout << "Dog()" << endl; throw 1; }
    ~Dog() { cout << "~Dog()" << endl; }
};

class UseResources
{
    class Cat cat;
    class Dog dog;

    public:
    UseResources() : cat(), dog() { cout << "UseResources()" << endl; }
    ~UseResources() { cout << "~UseResources()" << endl; }
};

int main()
{
    try
    {
        UseResources ur;
    }
    catch( int )
    {
        cout << "Inside handler" << endl;
    }
}

Now, if we replace the definition of the UseResources() constructor, with one that uses a function try block, as below,

UseResources() try : cat(), dog() { cout << "UseResources()" << endl; } catch(int) {}

the output is the same

Cat()  
Dog()  
~Cat()  
Inside handler 

i.e., with exactly the same final result.

What is then, the purpose of a function try block ?

  • 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-27T05:00:24+00:00Added an answer on May 27, 2026 at 5:00 am

    Imagine if UseResources was defined like this:

    class UseResources
    {
        class Cat *cat;
        class Dog dog;
    
        public:
        UseResources() : cat(new Cat), dog() { cout << "UseResources()" << endl; }
        ~UseResources() { delete cat; cat = NULL; cout << "~UseResources()" << endl; }
    };
    

    If Dog::Dog() throws, then cat will leak memory. Becase UseResources‘s constructor never completed, the object was never fully constructed. And therefore it does not have its destructor called.

    To prevent this leak, you must use a function-level try/catch block:

    UseResources() try : cat(new Cat), dog() { cout << "UseResources()" << endl; } catch(...)
    {
      delete cat;
      throw;
    }
    

    To answer your question more fully, the purpose of a function-level try/catch block in constructors is specifically to do this kind of cleanup. Function-level try/catch blocks cannot swallow exceptions (regular ones can). If they catch something, they will throw it again when they reach the end of the catch block, unless you rethrow it explicitly with throw. You can transform one type of exception into another, but you can’t just swallow it and keep going like it didn’t happen.

    This is another reason why values and smart pointers should be used instead of naked pointers, even as class members. Because, as in your case, if you just have member values instead of pointers, you don’t have to do this. It’s the use of a naked pointer (or other form of resource not managed in a RAII object) that forces this kind of thing.

    Note that this is pretty much the only legitimate use of function try/catch blocks.


    More reasons not to use function try blocks. The above code is subtly broken. Consider this:

    class Cat
    {
      public:
      Cat() {throw "oops";}
    };
    

    So, what happens in UseResources‘s constructor? Well, the expression new Cat will throw, obviously. But that means that cat never got initialized. Which means that delete cat will yield undefined behavior.

    You might try to correct this by using a complex lambda instead of just new Cat:

    UseResources() try
      : cat([]() -> Cat* try{ return new Cat;}catch(...) {return nullptr;} }())
      , dog()
    { cout << "UseResources()" << endl; }
    catch(...)
    {
      delete cat;
      throw;
    }
    

    That theoretically fixes the problem, but it breaks an assumed invariant of UseResources. Namely, that UseResources::cat will at all times be a valid pointer. If that is indeed an invariant of UseResources, then this code will fail because it permits the construction of UseResources in spite of the exception.

    Basically, there is no way to make this code safe unless new Cat is noexcept (either explicitly or implicitly).

    By contrast, this always works:

    class UseResources
    {
        unique_ptr<Cat> cat;
        Dog dog;
    
        public:
        UseResources() : cat(new Cat), dog() { cout << "UseResources()" << endl; }
        ~UseResources() { cout << "~UseResources()" << endl; }
    };
    

    In short, look on a function-level try-block as a serious code smell.

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

Sidebar

Related Questions

Possible Duplicate: Difference between try-catch syntax for function What is the difference in the
Possible Duplicate: Caller function in PHP 5? Like this: function foo(){ do_something(); } function
Possible Duplicate: Function to Clear the Console in R In bash, its like this:
Possible Duplicate: JavaScript function aliasing doesn't seem to work Why doesn't this work? function
Possible Duplicate: What does (function($) {})(jQuery); mean? I've seen a lot of jQuery code
Possible Duplicate: JavaScript: immediate function invocation syntax A similar question has been asked here
Possible Duplicate: JavaScript Function Definition in ASP User Control Hi, I have a generic
Possible Duplicate: Faster DirectoryExists function? I want to check if some file exists on
Possible Duplicate: JavaScript: var functionName = function() {} vs function functionName() {} What's the
Possible Duplicate: JavaScript: Why the anonymous function wrapper? I would like to ask you

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.