More Exception Features
Although the throw
-catch
mechanism is similar to function arguments and the function return mechanism, there are a few differences. One, which you’ve already encountered, is that a return statement in a function fun()
transfers execution to the function that called fun()
, but a throw
transfers execution all the way up to the first function having a try
-catch
combination that catches the exception. For example, in Listing 15.12, when hmean()
throws an exception, control passes up to means()
, but when gmean()
throws an exception, control passes up to main()
.
Another difference is that the compiler always creates a temporary copy when throwing an exception, even if the exception specifier and catch
blocks specify a reference. ...
Get C++ Primer Plus 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.