Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try\catch block in the main() function without brackets

Tags:

c++

try-catch

Visual Studio 2015; C++ language.

I remember that I read somewhere about the entry point (i.e. main method) what it is possible to write this:

#include <iostream>
using namespace std;

int main()
try{
  return 0; // I am here...
}
catch (...){
  cout << "I am 'catch'..." << endl; // This row wasn't called!
  return 1; // Oops... But the next `F10` key pressing jumps from the "try" 
  // block into this row!
}

I.e. at this case the try\catch block is located not in the brackets:

int main() { // start bracket
  try{
    return 0;
  }
  catch (...){
    return 1;
  }
} // end bracket

Both cases are compiled successfully and work too, but... In the first variant, when I am step by step pressing the F10 key after the try block I get into the catch block also. For the second variant of code I haven't such behaviour.

Why does it happen?

like image 336
Andrey Bushman Avatar asked Sep 30 '15 13:09

Andrey Bushman


People also ask

Can we use try catch in main method?

It must be used within the method. If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception. Java try block must be followed by either catch or finally block.

Can we have catch () without try?

The finally block always executes when the try block exits. So you can use finally without catch but you must use try.

What is the function of try catch block?

The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Can we have try finally without catch in JavaScript?

However, code like try {} fails because it is missing an (unused) catch {} block.


1 Answers

Your construction is a function-try-block and is defined in drafs n4296 for C++ 11 specification at 8.4 Function definitions [dcl.fct.def.general] with:

Function definitions have the form

  • function-definition:
    • attribute-specifier-seqopt decl-specifier-seqopt declarator virt-specifier-seqopt function-body
  • function-body:
    • ctor-initializeropt compound-statement
    • function-try-block
    • = default ;
    • = delete ;

and later in 15 Exception handling [except] with:

function-try-block:

  • try ctor-initializeropt compound-statement handler-seq

Examples suggest that the normal usage for a function-try-block should be a ctor, but it is valid for a normal function (and main is syntactically a mere function)

It is valid and works normally, meaning that catch block is only evaluated if an exception occurs in the ctor-initializeropt on in the compound-statement. You can confirm it in your code by adding prints in your blocks or by testing return value.

In a Unix like system

foo
echo $?

should echo 0

In a Windows system under a CMD.exe windows

foo.exe
if errorlevel 1 echo "Catch block"

should not output Catch block

If your debugger lets you execute instructions in the catch block... it is not C++ 11 conformant!

But it is known that when exiting a block, MSVC debugger puts cursor on last line of block, I assume that is is what is happening here because the last line of the function-try-block is last line of catch.

like image 92
Serge Ballesta Avatar answered Sep 20 '22 07:09

Serge Ballesta