异常的多态使用 提供基类异常类 class BaseException纯虚函数 virtual void printError() = 0; 子类空指针异常 和 越界异常 继承 BaseException重写virtual void printError()测试 利用父类引用指向子类对象 登录后复制 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; //异常的基类 class BaseException { public: virtual void printError() = 0; }; //空指针异常 class NULLPointerException :public BaseException { public: virtual void printError() { cout << "空指针异常" << endl; } }; //越界异常 class OutOfRangeException :public BaseException { public: virtual void printError() { cout << "越界异常" << endl; } }; void doWork() { //throw NULLPointerException(); throw OutOfRangeException(); } void test01() { try { doWork(); } catch (BaseException & e) { e.printError(); } } int main(){ test01(); system("pause"); return EXIT_SUCCESS; } 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58. 原创作者: zaishu 转载于: https://blog.51cto.com/zaishu/11898428