Function try blocks

You may decide that you want to protect an entire function with a try block, in which case you could write code like this:

    void test(double d)     {         try         {             cout << setw(10) << d << setw(10) << reciprocal(d) << endl;         }           catch (exception& e)         {             cout << "error: " << e.what() << endl;         }     }

This uses the reciprocal function, as defined earlier, that will throw an exception if the parameter is zero. An alternative syntax for this is:

    void test(double d)     try     {         cout << setw(10) << d << setw(10) << reciprocal(d) << endl;     }     catch (exception& e)     {         cout << "error: " << e.what() << endl;     }

This looks rather odd because the function prototype is followed immediately by the try... catch block and there is no outer set ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.