#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++ 多态在异常中的使用
猜你喜欢
转载自blog.csdn.net/weixin_43903378/article/details/104056875
今日推荐
周排行