기본 자료형 말고도 클래스의 객체도 예외 데이터가 될 수 있고 또 이것이 보다 일반적인 방법이다.
class DepositException
{
private:
int reqDep;
public:
DepositException(int money) : reqDep(money)
{}
void ShowExceptionReason()
{
...
}
}
class WIthdrawException
{
private:
...
public:
...
}
class Account
{
private:
char accNum[50];
int balance;
public:
...
}
int main(void)
{
try
{
myAcc.Deposit(2000);
myAcc.Deposit(-300);
}
catch(DepositException &expn)
{
expn.ShowExceptionReason();
}
...
}
예외 클래스도 상속의 관계를 구성할 수 있다. 앞서 예제 ATMSim.cpp 에서 보인 두 예외 클래스는 다음과 같이 상속의 관계로 묶을 수 있다.
class AccountException
{
public:
virtual void ShowExceptionReason() = 0; //순수 가상함수
}
class DepositException : public AccountException
class WithdrawException : public AccountException
try
{}
catch () {}
catch () {}
catch () {}
try-catch 문은 if 문과 비슷하게 작동함으로 배치에 신경써야된다.