c++异常处理(一)

什么是异常?

    比如当程序要求输入一个数字,如果输入数字就会运行很好,但是当输入一个字符的时候程序就会崩溃;当内存不够程序使用,或者硬盘的某个扇区出现坏道导致程序不能写入,以及U盘未插好,都会出错。

而c++提供了处理异常的方法:

try

{

         wrong();

}


catch(outof memory)

{

//发送错误信息给用户

}

下面给出一个程序,来说明如何使用异常处理:

#include<iostream>
using namespace std;
const int num = 5;
class people
{

public:
	people(int size = num);
	~people(){delete []p;}
	int &operator[](int off);
	const int &operator[](int off)const;
	int GetSize()const {return size;}

	class wrong{};
	class Zero{};
	class Small{};
	class Big{};
	class Nav{};

private:
	int *p;
	int size;

};
people::people(int Size):size(Size)
{

	cout<<"调用构造函数\n";

	if(Size == 0)
	{
	
		throw Zero();
	}
	if(Size < 10)
	{
	
		throw Small();
	}
	if(Size> 10000)
	{
	
		throw Big();
	}
	if(Size < 0)
	{
	
		throw Nav();
	}

	p = new int[size];

	for(int i=0;i<size;i++)
	{
	
		p[i] = 0;
	}



}

int &people::operator [](int off)
{

	if(off >= 0 && off < GetSize())
	{
	
		return p[off];
	}

	throw wrong();
	return p[0];


}
const int &people::operator [](int off) const
{
		if(off >= 0 && off < GetSize())
	{
	
		return p[off];
	}

	throw wrong();
	return p[0];


}






int main()
{

	try
	{
	
		people one(60000);
		for(int i = 0; i< 100; i++)
		{
		
			one[i] = i;
			cout<<"one[i]赋值完毕"<<endl;
		}
	}

	catch(people::wrong)
	{
	
		cout<<"超过数组长度,不能继续赋值操作\n";
	
	}

	catch(people::Big)
	{
	
		cout<<"下标值太大\n";
	}

	catch(people::Small)
	{
	
			cout<<"下标值太小\n";

	}

	catch(people::Zero)
	{
	
		cout<<"下标值为0\n";
	}

	catch(people::Nav)
	{
	
		cout<<"下标值是负数\n";
	}

	cout<<"程序结束\n";
	return 0;
}


程序执行的结果如下:


当程序运行正确时不会抛出异常,不正常时才会抛出异常。catch会在适当的时候对象的内存,并通知用户。由于现实程序中有多重错误,所以本程序使用了多个catch语句。

既然异常类是一个普通的类,那么它就支持虚函数和派生。程序如下:

#include<iostream>
using namespace std;

const int num = 5;
class people
{

public:
	people(int size = num);
	~people(){delete []p;};
	int &operator[](int off);
	const int &operator[](int off) const;
	int GetSize()const { return size; }
	class wrong{};
	class offset
	{
	
	public:
		offset(int Size):size(Size)
		{
		
		}
		~offset(){ }
		virtual int Get()
		{
		
			return size;
		}

		virtual void show()
		{
		
			cout<<"抛出offset异常\n";
			cout<<"下标值"<<size<<"出错"<<endl;
		}
	protected:
		int size;



	};

	class Big:public offset
	{
	
	public:
		Big(int Size):offset(Size){ }
		virtual void show()
		{
		
			cout<<"抛出Big异常\n";
			cout<<"下标值"<<offset::size<<"出错"<<endl;
		
		}
	
	};
	class Nav : public offset
	{
	
	public:
		Nav(int Size):offset(Size){ }
		virtual void show()
		{
		
			cout<<"抛出Nav异常\n";
			cout<<"下标值"<<offset::size<<"出错"<<endl;
		
		}
	
	};

	class Small
	{
	
	public:
		Small(int Size):size(Size){  }
		~Small(){}
		virtual int get(){return size;}

		virtual void show()
		{
		
			cout<<"抛出Small异常\n";
			cout<<"下标值"<<size<<"出错"<<endl;

		
		}
	protected:
		int size;
	
	};

	class Zero: public Small
	{
	public:
	
		Zero(int Size):Small(Size) {}

		virtual void show()
		{
		
			cout<<"抛出Zero异常\n";
			cout<<"下标值"<<Small::size<<endl;

		
		}
	
	};




	private:
		int *p;
		int size;



};

people::people(int Size):size(Size)
{

	cout<<"调用构造函数\n";
	if(Size == 0)
	{
	
		throw Zero(Size);
	}
	if(Size < 10)
	{
	
		throw Small(Size);
	}

	if(Size >10000)
	{
	
		throw Big(Size);
	}
	if(Size < 1)
	{
	
		throw Nav(Size);
	}

	p = new int[size];
	for(int i = 0;i < size; i++ )
	{
	
		p[i] = 0;
	}






}

int &people::operator [](int off)
{
	
	if(off >= 0 && off < GetSize() )
		{
		
			return p[off];
		}

		throw wrong();
		return p[0];

}


const int &people::operator [](int off) const
{
	if(off >= 0 && off < GetSize() )
		{
		
			return p[off];
		}

		throw wrong();
		return p[0];



}



int main()
{

	try
	{
		people one(0);
		for(int i=0;i<100;i++ )
		{
		
			one[i] = i;
			cout<<"one[<<i<<]赋值完毕..."<<endl;
		}

	
	}

	catch (people::wrong)
	{
	
		cout<<"超出数组长度,不能继续赋值\n";
	}

	catch(people::Small &small)
	{
	
		small.show();
	}
	catch (people::offset &off)
	{
	
		off.show();
	}

	catch(...)
	{
	
		cout<<"程序出现异常"<<endl;
	}


	return 0;
}


程序执行结果如下:


有了虚函数,提现了c++的多态性。


下面给出一个程序,是模板类与异常类的。程序如下:

#include<iostream>
using namespace std;

const int num = 5;
template<class T>
class people
{

public:
	people(int size = num);
	~people(){delete []p;};
	T &operator[](int off);
	const T &operator[](int off) const;
	int GetSize()const { return size; }
	class wrong{};
	class offset
	{
	
	public:
		offset(int Size):size(Size)
		{
		
		}
		~offset(){ }
		virtual int Get()
		{
		
			return size;
		}

		virtual void show()
		{
		
			cout<<"抛出offset异常\n";
			cout<<"下标值"<<size<<"出错"<<endl;
		}
	protected:
		int size;



	};

	class Big:public offset
	{
	
	public:
		Big(int Size):offset(Size){ }
		virtual void show()
		{
		
			cout<<"抛出Big异常\n";
			cout<<"下标值"<<offset::size<<"出错"<<endl;
		
		}
	
	};
	class Nav : public offset
	{
	
	public:
		Nav(int Size):offset(Size){ }
		virtual void show()
		{
		
			cout<<"抛出Nav异常\n";
			cout<<"下标值"<<offset::size<<"出错"<<endl;
		
		}
	
	};

	class Small
	{
	
	public:
		Small(int Size):size(Size){  }
		~Small(){}
		virtual int get(){return size;}

		virtual void show()
		{
		
			cout<<"抛出Small异常\n";
			cout<<"下标值"<<size<<"出错"<<endl;

		
		}
	protected:
		int size;
	
	};

	class Zero: public Small
	{
	public:
	
		Zero(int Size):Small(Size) {}

		virtual void show()
		{
		
			cout<<"抛出Zero异常\n";
			cout<<"下标值"<<Small::size<<endl;

		
		}
	
	};




	private:
		int *p;
		int size;



};
template<class T>

people<T>::people(int Size):size(Size)
{

	cout<<"调用构造函数\n";
	if(Size == 0)
	{
	
		throw Zero(Size);
	}
	if(Size < 10)
	{
	
		throw Small(Size);
	}

	if(Size >10000)
	{
	
		throw Big(Size);
	}
	if(Size < 1)
	{
	
		throw Nav(Size);
	}

	p = new T[size];
	for(int i = 0;i < size; i++ )
	{
	
		p[i] = 0;
	}






}

template<class T>

T &people<T>::operator [](int off)
{
	
	if(off >= 0 && off < GetSize() )
		{
		
			return p[off];
		}

		throw wrong();
		return p[0];

}
template<class T>



const T &people<T>::operator [](int off) const
{
	if(off >= 0 && off < GetSize() )
		{
		
			return p[off];
		}

		throw wrong();
		return p[0];



}



int main()
{

	try
	{
		people<int> one(0);
		for(int i=0;i<100;i++ )
		{
		
			one[i] = i;
			cout<<"one[<<i<<]赋值完毕..."<<endl;
		}

	
	}

	catch (people<int>::wrong)
	{
	
		cout<<"超出数组长度,不能继续赋值\n";
	}

	catch(people<int>::Small &small)
	{
	
		small.show();
	}
	catch (people<int>::offset &off)
	{
	
		off.show();
	}

	catch(...)
	{
	
		cout<<"程序出现异常"<<endl;
	}


	return 0;
}

程序执行结果如下:


本篇博文只是介绍了异常处理的部分,下一篇博文继续介绍异常处理机制。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值