본문 바로가기

PROGRAMMING LANGUAGE/C++

상속과 예외처리의 관계

#include <iostream>
using namespace std;

class AAA
{
public:
	void ShowYou() { cout << "AAA exception!" << endl; }
};

class BBB : public AAA
{
public:
	void ShowYou() { cout << "BBB exception!" << endl; }
};

class CCC : public BBB
{
public:
	void ShowYou() { cout << "CCC exception!" << endl; }
};

void ExceptionGenerator(int expn)
{
	if (expn == 1)			throw AAA();
	else if (expn == 2)		throw BBB();
	else					throw CCC();
}


int main(void)
{
	try
	{
		ExceptionGenerator(3);
		ExceptionGenerator(2);
		ExceptionGenerator(1);
	}
	catch (CCC& expn)
	{
		cout << "catch(CCC& expn)" << endl;		expn.ShowYou();
	}
	catch (BBB& expn)
	{
		cout << "catch(BBB& expn)" << endl;		expn.ShowYou();
	}
	catch (AAA& expn)  
	{
		cout << "catch(AAA& expn)" << endl;		expn.ShowYou();
	}
	
	
	return 0;
}

'PROGRAMMING LANGUAGE > C++' 카테고리의 다른 글

템플릿 특수화  (0) 2021.12.03
템플릿 함수 사용하기  (0) 2021.12.02
c++예외처리  (0) 2021.12.01
버블정렬을 클래스 이용해서 구현하기  (0) 2021.12.01
스마트포인터  (0) 2021.11.30