C ++ polymorphism exception of using

#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;
}
Published 100 original articles · won praise 6 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43903378/article/details/104056875