|
11 | 11 | Exception handling provides a way of transferring control and information |
12 | 12 | from a point in the execution of a thread to an exception handler |
13 | 13 | associated with a point previously passed by the execution. |
14 | | -A handler will be invoked only by a |
15 | | -\grammarterm{throw-expression} |
16 | | -invoked in code executed in the handler's try block |
| 14 | +A handler will be invoked only by throwing an exception |
| 15 | +in code executed in the handler's try block |
17 | 16 | or in functions called from the handler's try block . |
18 | 17 |
|
19 | 18 | \indextext{\idxcode{try}}% |
|
65 | 64 | \indextext{try block|see{exception handling, try block}}% |
66 | 65 | \indextext{handler|see{exception handling, handler}}% |
67 | 66 | A \grammarterm{try-block} is a \grammarterm{statement} (Clause~\ref{stmt.stmt}). |
68 | | -A \grammarterm{throw-expression} is of type \tcode{void}. Code that executes a |
69 | | -\grammarterm{throw-expression} is said to ``throw an exception;'' code that |
70 | | -subsequently gets control is called a ``handler.'' \enternote Within this Clause |
| 67 | +A \grammarterm{throw-expression} is of type \tcode{void}. \enternote Within this Clause |
71 | 68 | ``try block'' is taken to mean both \grammarterm{try-block} and |
72 | 69 | \grammarterm{function-try-block}. \exitnote |
73 | 70 |
|
|
199 | 196 |
|
200 | 197 | \pnum |
201 | 198 | Throwing an exception transfers control to a handler. |
| 199 | +\enternote |
| 200 | +An exception can be thrown from one of the following contexts: |
| 201 | +\grammarterm{throw-expression} (see below), allocation |
| 202 | +functions~(\ref{basic.stc.dynamic.allocation}), |
| 203 | +\tcode{dynamic_cast}~(\ref{expr.dynamic.cast}), |
| 204 | +\tcode{typeid}~(\ref{expr.typeid}), |
| 205 | +\grammarterm{new-expression}~(\ref{expr.new}), and standard library |
| 206 | +functions~(\ref{structure.specifications}). |
| 207 | +\exitnote |
202 | 208 | An object is passed and the type of that object determines which handlers |
203 | 209 | can catch it. |
204 | 210 | \enterexample |
|
255 | 261 | keyword was most recently entered by the thread of control and not yet exited. |
256 | 262 |
|
257 | 263 | \pnum |
258 | | -A |
259 | | -\grammarterm{throw-expression} |
260 | | -copy-initializes~(\ref{dcl.init}) a temporary object, |
| 264 | +Throwing an exception |
| 265 | +copy-initializes~(\ref{dcl.init}, \ref{class.copy}) a temporary object, |
261 | 266 | called the |
262 | | -\indextext{exception handling!exception object}\term{exception object}, |
263 | | -the type of which |
264 | | -is determined by removing any top-level |
265 | | -\grammarterm{cv-qualifier}{s} |
266 | | -from the static type of the operand of |
267 | | -\tcode{throw} |
268 | | -and adjusting the type from ``array of |
269 | | -\tcode{T}'' |
270 | | -or ``function returning |
271 | | -\tcode{T}'' |
272 | | -to ``pointer to |
273 | | -\tcode{T}'' |
274 | | -or ``pointer to function |
275 | | -returning |
276 | | -\tcode{T}'', |
277 | | -respectively. |
| 267 | +\indextext{exception handling!exception object}\term{exception object}. |
278 | 268 | The temporary is an lvalue and is used to initialize the |
279 | 269 | variable named in the matching |
280 | 270 | \term{handler}~(\ref{except.handle}). |
281 | 271 | If the type of the exception object would |
282 | 272 | be an incomplete type or a pointer to an incomplete |
283 | 273 | type other than (possibly cv-qualified) |
284 | 274 | \tcode{void} the program is ill-formed. |
285 | | -Except for these restrictions and the restrictions on type matching mentioned |
286 | | -in~\ref{except.handle}, the operand of |
287 | | -\tcode{throw} |
288 | | -is treated exactly as a function argument in a call~(\ref{expr.call}) or the operand |
289 | | -of a return statement. |
| 275 | +Evaluating a \grammarterm{throw-expression} with an operand throws an |
| 276 | +exception; the type of the exception object is determined by removing |
| 277 | +any top-level \grammarterm{cv-qualifiers} from the static type of the |
| 278 | +operand and adjusting the type from ``array of \tcode{T}'' or ``function |
| 279 | +returning \tcode{T}'' to ``pointer to \tcode{T}'' or ``pointer to function returning |
| 280 | +\tcode{T},'' respectively. |
290 | 281 |
|
291 | 282 | \pnum |
292 | 283 | \indextext{exception handling!memory}% |
|
308 | 299 | The implementation may then |
309 | 300 | deallocate the memory for the exception object; any such deallocation |
310 | 301 | is done in an unspecified way. |
311 | | -\enternote an exception thrown by a \grammarterm{throw-expression} does not |
| 302 | +\enternote a thrown exception does not |
312 | 303 | propagate to other threads unless caught, stored, and rethrown using |
313 | 304 | appropriate library functions; see~\ref{propagation} and~\ref{futures}. \exitnote |
314 | 305 |
|
|
358 | 349 | A |
359 | 350 | \grammarterm{throw-expression} |
360 | 351 | with no operand rethrows the currently handled exception~(\ref{except.handle}). |
361 | | -The exception is reactivated with the existing temporary; |
362 | | -no new temporary exception object is created. |
| 352 | +The exception is reactivated with the existing exception object; |
| 353 | +no new exception object is created. |
363 | 354 | The exception |
364 | 355 | is no longer considered to be caught; therefore, the value |
365 | 356 | of |
|
398 | 389 | \indextext{destructor!exception~handling|see{exception handling, constructors and destructors}} |
399 | 390 |
|
400 | 391 | \pnum |
401 | | -As control passes from a |
402 | | -\grammarterm{throw-expression} |
| 392 | +As control passes from the point where an exception is thrown |
403 | 393 | to a handler, |
404 | 394 | destructors are invoked for all automatic objects constructed since the |
405 | 395 | try block was entered. |
|
427 | 417 | \pnum |
428 | 418 | \indextext{unwinding!stack}% |
429 | 419 | The process of calling destructors for automatic objects constructed on the |
430 | | -path from a try block to a |
431 | | -\grammarterm{throw-expression} |
| 420 | +path from a try block to the point where an exception is thrown |
432 | 421 | is called |
433 | 422 | ``\term{stack unwinding}.'' |
434 | 423 | If a destructor called during stack unwinding exits with an exception, |
|
712 | 701 | within the handler. |
713 | 702 |
|
714 | 703 | \pnum |
715 | | -When the handler declares a non-constant object, |
716 | | -any changes to that object will not affect the temporary object |
717 | | -that was initialized by execution of the |
718 | | -\grammarterm{throw-expression}. |
719 | | -When the handler declares a reference to a non-constant object, |
| 704 | +When the handler declares an object, |
| 705 | +any changes to that object will not affect the exception object. |
| 706 | +When the handler declares a reference to an object, |
720 | 707 | any changes to the referenced object are changes to the |
721 | | -temporary object initialized when the |
722 | | -\grammarterm{throw-expression} |
723 | | -was executed and will have effect should that object be rethrown.% |
| 708 | +exception object and will have effect should that object be rethrown.% |
724 | 709 | \indextext{exception handling!handler!match|)}% |
725 | 710 | \indextext{exception handling!handler|)} |
726 | 711 |
|
|
0 commit comments