Rethrowing exceptions

前端 未结 3 1001
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 11:17

Why doesn\'t the following doesn\'t handle the exception that was rethrown? I tried all the combinations but none of them would show the output in last catch so I\'m confused!

3条回答
  •  礼貌的吻别
    2021-02-13 11:53

    The re-throw is not handled by the same try-catch block. It's thrown up to the calling scope.

    In [except.throw] (2003 wording):

    A throw-expression with no operand rethrows the exception being handled.

    and:

    When an exception is thrown, control is transferred to the nearest handler with a matching type (15.3); “nearest” means the handler for which the compound-statement, ctor-initializer, or function-body following the try keyword was most recently entered by the thread of control and not yet exited.

    Your try block has exited, so its handlers are not candidates. Thus, none of the catch blocks in your code may handle the re-throw.

    Admittedly this is rather confusing wording.

提交回复
热议问题