企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
~~~ //异常的基类 class Father { public: virtual void printM() { } }; //1.有继承 class SonNULL : public Father { public: virtual void printM()//2.重写父类的虚函数 { cout << "空指针异常" << endl; } }; class SonOut : public Father { public: virtual void printM() { cout << "越位溢出" << endl; } }; void func(int a, int b) { if (a == 0) { throw SonNULL(); } if (b == 0) { throw SonOut(); } } void test02() { int a = 0; int b = 10; try { func(a, b); } catch (Father &f)//3.父类引用指向子类对象 { f.printM(); } } ~~~