#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//异常 基类
class BaseException
{
public:
virtual void printError() = 0;
};
//空指针 异常
class NULLPointException :public BaseException
{
public:
virtual void printError()
{
cout << "空指针异常" << endl;
}
};
//越界异常
class OutOfRangeException :public BaseException
{
public:
virtual void printError()
{
cout << "越界异常" << endl;
}
};
void doWork()
{
//throw NULLPointException();
throw OutOfRangeException();
}
void test01()
{
try
{
doWork();
}
catch ( BaseException & e)
{
e.printError();
}
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
C++ 多态在异常中的使用
最新推荐文章于 2021-11-01 11:11:34 发布