2

I'm reading Stroustrup's Programming: Principles and Practice and came across this code:

int main()
try {
    // our program
    return 0; //0 indicates success
}
catch (exception& e) {
    cerr << "error: " <<e.what() <<'\n';
    keep_window_open();
    return 1; // 1 indicates failure
}
catch (...) {
    cerr << "Oops: Unknown exception!\n";
    keep_window_open();
    return 2; //2 indicates failure
}

At first I thought that the first line called the main function (which contained the heart of the code and was written somewhere else). And then we are just trying to catch errors that might have occurred inside of main(). But if that's the case, why does it say "our program" in the try block? This makes me think that this code is defining main(). But if that's the case, then where are the bracket s? That is, why isn't it int main() { on the first line?

Xu Wang
  • 10,199
  • 6
  • 44
  • 78
  • Possible duplicate of [What is the function try block handler?](http://stackoverflow.com/questions/3633419/what-is-the-function-try-block-handler) – In silico Feb 23 '12 at 04:55
  • It's not. He's confused about the lack of {}'s for main, not the try block – colithium Feb 23 '12 at 05:49
  • possible duplicate of [When is a function try block useful?](http://stackoverflow.com/questions/5612486/when-is-a-function-try-block-useful) – Cody Gray - on strike Feb 24 '12 at 03:37

2 Answers2

5
int main()
try
{

}
catch (...)
{

}
...optionally more catches...

...is just a shorter notation equivalent to...

int main()
{
    try
    {

    }
    catch (...)
    {

    }
    ...optionally more catches...
}

Why does it exist? Well:

  • As soon as you see the function, you understand that a try/catch block encompasses all the function content, whereas in the second form you have to scan down to the end to see if there's anything after the catches. This insight makes it quicker and easier to reason about the exception handling.

  • With the second notation, another programmer's more likely to unwittingly insert code before or after the try/catch block, which may invalidate earlier exception handling design. A try/catch block makes it more obvious that "encompassing" exception handling is a deliberate design decision.

  • It's more concise.

  • If you have an existing function and want to add exception handling to it, then using the function try block notation clearly avoids having to indent everything inside the function to continue to comply with normal indentation practices. Indenting an entire function is painful, especially if you observe an 80 (or whatever) character line length and have to manually wrap long lines to get them formatted the way you want. (Similarly, if you had a catch statement then decide to let the exceptions directly reach the caller - obviously more useful for functions other than main - you can remove the try/catch without needing to unindent the try { } content.)

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • Thank you for the in-depth explanation. That makes sense and now I understand the writing of it that way. I guess I should have also realized that `int main()` is not terminated with a colon so it could not be calling the function. – Xu Wang Feb 23 '12 at 05:15
1

It defines the main() function using function-try-block:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • So it is possible to define a function without braces? In which cases can i do that? I guess that is where I am confused. – Xu Wang Feb 23 '12 at 05:00
  • @XuWang: Yes. That is what function-try-block is about. Go through the link, it explains lots of things about the syntax. – Nawaz Feb 23 '12 at 05:01
  • It's not without braces. The `try` keyword and the entire catch clauses are outside the function's body. – bames53 Feb 23 '12 at 05:03
  • @bames53 could you expand? So the function body of `main()` is presumed to be elsewhere? That is, the `try` and `catch` are not in the main function? – Xu Wang Feb 23 '12 at 05:04
  • 1
    @XuWang well, if you look at the grammar technically the `try` and catch clauses are part of the body, but I always think of them as decoration around the function's body. Like the `const` in a const member function declaration: `int A::foo() const { }` – bames53 Feb 23 '12 at 05:13
  • Ah, I see what you mean now. That makes sense. Thank you. – Xu Wang Feb 23 '12 at 05:16