[C++复习]09|Exception handling

[note 1]

general form of the try and catch

try
{
	//
}
catch (type arg)
{
	//	
}

[example 1]

//Throw point outside the try block
#include<iostream>
using namespace std;

void divide(int x, int y, int z)
{
	cout << "\nWe are inside the function \n";
	if((x-y)!=0)			//It is OK
	{
		int R=z/(x-y);
		cout << "Result = " << R << "\n";
	}
	else					//There is a problem
	{	
		throw(x-y);			//Throw point
	}
}

int main()
{
	try
	{
		cout << "We are inside the try block \n";
		divide(10,20,30);	//Invoke divide()
		divide(10,10,20);	//Invoke divide()
	}
	catch(int i)			//Catches the exception
	{
		cout << "Caught the exception \n";
	}
	return 0;
}

[note 2]

catch all exceptions

catch(...)
{
	//	
}

[note 3]

rethrow

#include<iostream>
using namespace std;

void divide(double x, double y)
{
    cout << "Inside function \n";
    try
    {
        if(y==0.0)
            throw y;        //Throwing double
        else
            cout << "Division = " << x/y << "\n";
    }
    catch(double)            //Catch a double
    {
        cout << "Caught double inside function \n";
        throw;                //Rethrowing double
    }
    cout << "End of function \n\n";
}

int main()
{
    cout << "Inside main \n";
    try
    {
        divide(10.5,2.0);
        divide(20.0,0.0);
    }
    catch(double){cout << "Caught double inside main \n";}
    cout << "End of main \n";
    
    return 0;
}


output:

Inside main 
Inside function 
Division = 5.25
End of function 

Inside function 
Caught double inside function 
Caught double inside main 
End of main 

[note 4]

a nested try-catch program

#include<iostream>
using namespace std;

void fc()
{
    try{throw "help...";}
    catch(int x){cout<<"in fc..int hanlder"<<endl;}
    try{cout<<"no error handle..."<<endl;}
    catch(char *px){cout<<"in fc..char* hanlder"<<endl;}
}
void fb()
{
    int *q=new int[10];
    try
    {
        fc();
        cout<<"return form fc()"<<endl;
    }
    catch(...)
    {
        delete []q;
        throw;
    }
}
void fa()
{
    char *p=new char[10];
    try{
        fb();
        cout<<"return from fb()"<<endl;
    }
    catch(...)
    {
        delete []p;
        throw;
    }
}

int main()
{
    try
    {
        fa();
        cout<<"return from fa"<<endl;
    }
    catch(...){cout<<"in main"<<endl;}
    cout<<"End"<<endl;
    
    return 0;
}


output:

in main
End

[note 5]

to restrict the throw type

type function(arg-list) throw (type-list)
{
	...	
}

[note 6]

Exception class

#include <iostream>
using namespace std;

const int MAX=3;
class Full
{
    int a;
public:
    Full(int i):a(i){}
    int getValue(){return a;}
};
class Empty{};
class Stack
{
    int s[MAX];
    int top;
public:
    Stack(){top=-1;}
    void push(int a)
	{
        if(top>=MAX-1)  
			throw Full(a);				//L1
        s[++top]=a;
    }
    int pop()
	{
        if(top<0)
			throw Empty();
        return s[top--];
    }
};

int main()
{
    Stack s;
    try{
         s.push(10);  
		 s.push(20);
         s.push(30);  
		 s.push(40);					//L2
    }
    catch(Full e)						//L3 catch(Full &e)
	{										
         cout<<"Exception: Stack Full..."<<endl;
         cout<<"The value not push in stack:  "<<e.getValue()<<endl; //L4
    }

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值