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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:25:27+00:00 2026-05-26T12:25:27+00:00

Possible Duplicate: Difference between try-catch syntax for function What is the difference in the

  • 0

Possible Duplicate:
Difference between try-catch syntax for function

What is the difference in the utility and behavior of these try-catch blocks? When would I prefer one form over the other?

int f() {
  try {
    ...
  } catch(...) {
    ...
  }
}

int f() try {
  ...
} catch (...) {
  ...
}
  • 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-26T12:25:28+00:00Added an answer on May 26, 2026 at 12:25 pm

    If the entire body must go into the exception block, I tend to prefer the 2nd form as it’s easier to read (e.g. one less indentation level).

    However, the main place where this makes a difference is in constructors. Consider:

    Foo::Foo ()
    try : member_with_throwing_constructor(initial_value)
    {
    }
    catch ( ... )
    {
         // will process exceptions thrown by `member_with_throwing_constructor`
    
         // this will be added by the compiler if you
         // don't finish by a `throw ...` statement.
         throw;
    }
    

    and

    Foo::Foo ()
        : member_with_throwing_constructor(initial_value)
    {
        try
        {
        }
        catch ( ... )
        {
            // does not catch exceptions thrown in constructor
            // of `member_with_throwing_constructor`.
        }
        // can keep processing if `catch` handler does not `return` or `throw`.
    }
    

    These two code snippets have radically different behavior. The first one will catch exceptions raised in data member constructors (usually through the initializer list, but default constructors apply too) and will automatically re-raise the exception because the instance could not be created safely. The second form does not cover data member initialization and allows you to choose whether to keep the object or not.

    I also like to add a global try-catch in main() to help with debugging uncaught exceptions. The following snippet:

    int main ( int, char ** )
    try
    {
        // main program...
    }
    catch ( const std::exception& error )
    {
        std::cerr << "Uncaught exception: '" << error << "'." << std::endl;
        return (EXIT_FAILURE);
    }
    catch ( ... )
    {
        std::cerr << "Uncaught exception of unknown type." << std::endl;
        return (EXIT_FAILURE);
    }
    

    Some people will argue that not catching the exceptions allows the program to crash and you can get a core dump to help with debugging. While this may be useful in debug mode, as it can help the debugger point you to the exact line of code that raised the exception, I like to ship programs that don’t crash, and I can show the user a message saying a bug report was submitted.

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

Sidebar

Related Questions

Possible Duplicate: When is a function try block useful? Difference between try-catch syntax for
Possible Duplicate: Difference between “var” and “object” in C# I would like to know
Possible Duplicate: Difference between (function(){})(); and function(){}(); I have seen it a lot by
Possible Duplicate: Difference between pointer variable and reference variable in C++ When should I
Possible Duplicate: Difference between pointer variable and reference variable in C++ I am reading
Possible Duplicate: What is the difference between a delegate and events? Possible Duplicate: Difference
Possible Duplicate: Ruby: difference between || and ‘or’ Using Ruby || and or are
Possible Duplicate: What is the difference between "typename" and "class" template parameters? When defining
Possible Duplicate: What is the difference between String.Empty and “” Is equivalent to String.Empty
Possible Duplicate: What is the difference between \r and \n? Hi, What is difference

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.