Rethrowing exceptions

前端 未结 3 1000
伪装坚强ぢ
伪装坚强ぢ 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:52

    This should work :

    Derived D;
    
    
    try{
    
        try {
            throw D;
        } catch ( const Derived &d) {
            throw;
        } catch (const Base &b) {
            cout << "caught!" << endl;
        }
    
    } catch (const Base &b) {
        cout << "caught here!" << endl;
    }
    

    As other said, the rethrow will rethrow the same exception out of the catch block.

提交回复
热议问题