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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:00:52+00:00 2026-05-23T22:00:52+00:00

I came across this syntax recently for try-catch for function. struct A { int

  • 0

I came across this syntax recently for try-catch for function.

struct A
{
  int a;

  A (int i) : a(i)  // normal syntax
  {
    try {}
    catch(...) {}
  }

  A ()   // something different
  try : a(0) {}
  catch(...) {}

  void foo ()  // normal function
  try {}
  catch(...) {}
};

Both syntax are valid. Is there any technical difference between these syntax apart from coding style ? Is one of the syntax superior to other by any aspect ?

  • 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-23T22:00:53+00:00Added an answer on May 23, 2026 at 10:00 pm

    The First Syntax:
    The scope of the try block starts after the Member Initialization list has been completed, So any exception thrown during Member Initialization will not be caught by this try-catch block.

    The second syntax:
    It ensures that if an exception gets thrown during Member Initialization list then you are able to catch the exception.

    The Third Syntax:
    It ensures that any exception thrown from betwen the starting brace of the try block inside the function body gets caught appropriately, It would mean any exception caused during the argument passing(if any can occur) will not be caught in this try-catch block.

    So yes they are disinctly different in what functionality they provide.


    EDIT:
    Some guidelines to be considered while using the second syntax(function-try-block) in constructors & destructors:

    As per the C++ Standard,

    If the catch block does not throw (either rethrow the original exception, or throw something new), and control reaches the end of the catch block of a constructor or destructor, then the original exception is automatically rethrown.

    In Simple words:
    A constructor or destructor function-try-block’s handler code MUST finish by emitting some exception.

    Guideline 1:
    Constructor function-try-block handlers have only one purpose — to translate an exception. (And maybe to do logging or some other side effects.) They are not useful for any other purpose.

    Throwing a exception from destructors is an bad idea, Take a look here to know why.
    Guideline 2:
    Destructor function-try-blocks have no practical use at all. There should never be anything for them to detect, and even if there were something to detect because of evil code, the handler is not very useful for doing anything about it because it can not suppress the exception.

    Guideline 3:
    Always clean up unmanaged resource acquisition in local try-block handlers within the constructor or destructor body, never in constructor or destructor function-try-block handlers.


    For Standardese Fans:

    C++ standard, clause 15.3, paragraph 15:

    If a return statement appears in a handler of the function-try-block of a constructor, the program is ill-formed.

    C++ standard, clause 15.3, paragraph 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. Otherwise, a function returns when control reaches the end of a handler for the function-try-block (6.6.3). Flowing off the end of a function-try-block is equivalent to a return with no value; this results in undefined behavior in a value-returning function (6.6.3).


    References:
    Have a look at this must read resource here for more details & explanation.

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

Sidebar

Related Questions

I recently came across this syntax, I am unaware of the difference. I would
I was going through Jeffrey Palermo's book and came across this syntax. private void
I came across this in the Python interpreter source code. void PyThread_delete_key_value(int key) {
I came across this line in some code and can't find the syntax defined
I came across this recently, up until now I have been happily overriding the
I've been developing with PHP for some years now, and recently came across this
I came across this syntax while reading a script. I am not sure what
I'm new to PHP. I came across this syntax in WordPress . What does
Came across this curiosity recently. One solution, with two projects within it (ORM and
I have been trying to learn Prolog and came across this syntax on some

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.