[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;
}

发布了51 篇原创文章 · 获赞 5 · 访问量 4203

猜你喜欢

转载自blog.csdn.net/qq_43519498/article/details/92970179