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

The Archive Base Latest Questions

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

A little known, but almost never used C++ feature is given a declaration: void

  • 0

A little known, but almost never used C++ feature is given a declaration:

void foo();

One possible, legal definition could be:

void foo() try {
  throw 42;
}
catch(...) {
}

Here the whole function implementation wrapped is within a try/catch pair, which seems to be similar to allowing this.

Is that legal to do for int main()? E.g.:

int main() try {
  throw 42;
}
catch(...) {
}

The rules for main, n3290 § 3.6.1 mostly talk about what arguments it should take and what it returns – they don’t seem to explicitly forbid it as they do with various other odd things (e.g. linkages) you might be tempted to try.

Is this legal and well defined?

  • 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-27T08:39:26+00:00Added an answer on May 27, 2026 at 8:39 am

    The standard does not forbid its usage within [basic.start.main], and, while forcing all implementations to support at least int main() {/*...*/ } and int main(int argc, char* argv[]) {/*...*/}, does not limit implementations to those two declarations (3.6.1, para. 2).

    From that in isolation, it would appear at the least that it is legal, though of course that relates only to function-declarations, not function-definitions.

    Reading on, [except.handle], paragraph 13 states the following:

    Exceptions thrown in destructors of objects with static storage
    duration or in constructors of namespace-scope objects are not caught
    by a function-try-block on main(). (15.3 para. 13)

    It makes specific mention of a function-try-block placed on main(), which strongly implies that such a structure is legal and has defined behavior. Adding in the information that main() is only special in its name and return type, and that implementations may not overload it to alter any behavior, makes a pretty strong case that it acts in a normal fashion except when specially noted such as in the above quote. In other words, yes, it is legal and well-defined.

    The blog post I supplied in the first version of this answer actually does a good job of illustrating the rules given by the above blockquote, so I’ll retain the link to it, even though it does not directly discuss the issue in the OP’s question.

    Regarding a comment on the OP, you can issue return statements within a function-try-block, and [except.handle] has this to say:

    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). (15.3 para. 15)

    If you’re in a catch-block at the end of main, you’re not going to flow over the function’s body (which would be the try-block in this case), so the rule that main automatically calls return 0; on flowover doesn’t apply. You need to return some int (quite possibly an error code) to keep from becoming undefined.

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

Sidebar

Related Questions

I write server code but have little to almost no JQ experience. I have
I'm an absolute noob at iPhone development but I know a little C. I
I am new to Perl, but I know a little bit of C though.
I know very little about JavaScript but despite this I'm trying to cobble something
I know a little about SNMP, but not enough. I need to develop an
I know a little bit of regex, but not mutch. What is the best
I'm a .NET developer who knows very little about Python, but want to give
I know it makes little difference to a project but, assuming you use #defined
I know this question might sound a little cheesy but this is the first
I know the question is a little choppy and perhaps misleading,but I have a

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.