Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The standard does not forbid its usage within [basic.start.main], and, while forcing all implementations to support at least <code>int main() {/*...*/ }</code> and <code>int main(int argc, char* argv[]) {/*...*/}</code>, does not limit implementations to those two declarations (3.6.1, para. 2).</p> <p>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.</p> <p>Reading on, [except.handle], paragraph 13 states the following: </p> <blockquote> <p>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)</p> </blockquote> <p>It makes specific mention of a <em>function-try-block</em> placed on <code>main()</code>, which strongly implies that such a structure is legal and has defined behavior. Adding in the information that <code>main()</code> 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, <strong>yes, it is legal and well-defined.</strong></p> <p>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 <a href="http://stupefydeveloper.blogspot.com/2008/10/c-function-try-catch-block.html">retain the link to it</a>, even though it does not directly discuss the issue in the OP's question.</p> <p>Regarding a comment on the OP, you can issue return statements within a <em>function-try-block</em>, and [except.handle] has this to say:</p> <blockquote> <p>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)</p> </blockquote> <p>If you're in a catch-block at the end of <code>main</code>, 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 <code>return 0;</code> on flowover doesn't apply. You need to return some <code>int</code> (quite possibly an error code) to keep from becoming undefined.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload