5

I read this code (by Bjarne Stroustrup). I am confused... The main function body is not in the {}, and function doesn't return value (as int). And it works... Why?

#include "std_lib_facilities.h" 

int main()
try
{
    cout<< "please enter two floating-point values separated by an operator\n The operator can be + - * or / : ";
    double val1 = 0;
    double val2 = 0;
    char op = 0;
    while (cin>>val1>>op>>val2) {   // read number operation number
        string oper;
        double result;
        switch (op) {
        case '+':
            oper = "sum of ";
            result = val1+val2; 
            break;
        case '-':
            oper = "difference between ";
            result = val1-val2; 
            break;
        case '*':
            oper = "product of ";
            result = val1*val2; 
            break;
        case '/':
            oper = "ratio of";
            if (val2==0) error("trying to divide by zero");
            result = val1/val2; 
            break;
        //case '%':
        //  oper = "remainder of ";
        //  result = val1%val2; 
        //  break;
        default:
                error("bad operator");
        }
        cout << oper << val1 << " and " << val2 << " is " << result << '\n';
        cout << "Try again: ";
    }
}
catch (runtime_error e) {   // this code is to produce error messages; it will be described in Chapter 5
    cout << e.what() << '\n';
    keep_window_open("~");  // For some Windows(tm) setups
}
catch (...) {   // this code is to produce error messages; it will be described in Chapter 5
    cout << "exiting\n";
    keep_window_open("~");  // For some Windows(tm) setups
}
Randy L
  • 14,384
  • 14
  • 44
  • 73
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182

1 Answers1

6

That code is using a Function Try Block, which is a special syntax allowing to embed the whole body of a function in a try/catch block (mostly useful for class constructors, in order to catch exceptions thrown by the constructors of base or member sub-objects).

Moreover, main() is the only value-returning function which is not required to return a value explicitly. When no return value is specified, 0 is assumed.

Per paragraph 3.6.1/5 of the C++11 Standard:

A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;
Community
  • 1
  • 1
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Just a question: Does the standard say anything about `0` being assumed, or will this be compiler dependent? – user123 May 15 '13 at 15:22
  • 1
    @Magtheridon96: It is specified by the Standard – Andy Prowl May 15 '13 at 15:22
  • 1
    @Magtheridon96: Added the quote to the answer – Andy Prowl May 15 '13 at 15:24
  • why the `keep_window_open("~");` code rows doesn't occur? It was must to show the prompt about the "~" pressing... The `std_lib_facilities.h` here: . – Andrey Bushman May 15 '13 at 15:29
  • @Bush: That function call is in a `catch` handler. If an exception is not thrown inside the `try` block, control will never be transferred to a `catch` handler and the program will simply terminate without exceptions – Andy Prowl May 15 '13 at 15:34
  • @Andy Prowl: Yes, I know. But I've break the cicle via the invalid data, and the exception must occur. – Andrey Bushman May 15 '13 at 15:37
  • @Bush: I can't try it myself since I do not have all the code, but if I were you I would try using a debugger and placing a breakpoint inside the `catch` block to verify whether control gets there. My bet is that it does – Andy Prowl May 15 '13 at 15:39
  • @Andy Prowl: Oh no... I am not right. I was inattentive. Thank you. – Andrey Bushman May 15 '13 at 15:39