函数级异常处理是如何工作的?

How function-level exception handling works?

有如下代码,来自这里

class Base {
    int i;
public:
    class BaseExcept {};
    Base(int i) : i(i) { throw BaseExcept(); }
};
class Derived : public Base {
public:
    class DerivedExcept {
        const char* msg;
    public:
        DerivedExcept(const char* msg) : msg(msg) {}
        const char* what() const { return msg; }
    };
    Derived(int j) try : Base(j) {  
        // Constructor body
        cout << "This won't print" << endl;
    }
    catch (BaseExcept&) {
        throw DerivedExcept("Base subobject threw");;
    }
};
int main() {
    try {
        Derived d(3);
    }
    catch (Derived::DerivedExcept& d) {
        cout << d.what() << endl;  // "Base subobject threw"
    }
}

我的问题是,为什么这里抛出的异常没有被捕获

catch (BaseExcept&) {
throw DerivedExcept("Base subobject threw");;
}

但是主要的收获是什么?

catch (Derived::DerivedExcept& d) {
cout << d.what() << endl; // "Base subobject threw"
}

为什么输出“Base subobject throw”

按照我的理解,抛出的异常应该在try之后的catch中捕获(或者相当于try的函数体),但是没有,为什么? 异常的传播路径是什么?

代码来自 there

我的问题是这样的,try catch我以前看过的都有这个表格

try{}
catch(){}

所以我认为下面的片段

     Derived(int j) try: Base(j) {
         // Constructor body
         cout << "This won't print" << endl;
     }
     catch (BaseExcept&) {
         throw DerivedExcept("Base subobject threw");;
     }

Derived()抛出异常时,应该被下面这行catch (BaseExcept&)捕获,而不是被main函数中的catch(Derived::DerivedExcept& d)捕获。 但是当我在这里注释掉它时

     catch (Derived::DerivedExcept& d) {
         //cout << d.what() << endl; // "Base subobject threw"
     }

不会有“Base subobject throwed”输出。 这与我的预期不同。我正在学习基本的异常知识。我是不是理解错了

Derived(int j) try : Base(j) {  
        // Constructor body
        cout << "This won't print" << endl;
    }
    catch (BaseExcept&) {      // why the thrown exception is not caught here
        throw DerivedExcept("Base subobject threw");;
    }

程序输出 Base subobject threw 的唯一方法是在您认为没有捕获异常的地方是否确实捕获了异常。该字符串在程序的其他任何地方都不存在。

But the catch in main?

那是因为你 throwcatch 中是个例外。该异常未被相同的 catch 捕获(在这种情况下会导致无限递归)。它被 main.

中的外部 try..catch 捕获