NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
~~~ //异常的基类 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(); } } ~~~