C++ -- 异常:try、throw、catch

2023-05-16

异常

申请内存的时候,内存不够用、空间配置器申请失败,不做处理、拷贝时内存错误、除数分母不能为0 等情况
会抛出异常
try…throw…catch

1、使用示例

1)除数为零,情况

未处理情况

对于以下代码,

void fn(int x,int y)
{
	/*在程序执行到此处时,如果y为0,会出现未被处理的异常,在运行时出现*/
	int t = x/y;
}
void main()
{
	fn(4,0);
}

在这里插入图片描述

在这里插入图片描述

进行处理

void fn(int x,int y)
{
	/*在程序执行到此处时,如果y为0,会出现未被处理的异常,在运行时出现*/
	//int t = x/y;
	try
	{
		if(y == 0)
			throw y;//抛出异常
		else
			cout << x/y << endl;	
	}
	catch(int n)//捕获抛出的异常
	{
		cout << "int n= " << n << endl;
	}
}
void main()
{
	fn(4,0);
}

运行结果:
在这里插入图片描述

2、接收所抛出异常的类型问题

按照抛出异常的类型,对应的捕获接收,如果没有对应的类型,则会被...接收
如果没有...处理,则不会接收异常,程序会崩溃
异常是按照最近的catch块接收,如果接收不住,则会向外继续抛出异常,由外面来接收
void fn(int x,int y)
{
	/*在程序执行到此处时,如果为0,会出现未被处理的异常,在运行时出现*/
	//int t = x/y;//error
	try
	{
		if(y == 0)
			throw y;//抛出异常
		else
			cout<<x/y<<endl;
	}
	/*
	按照抛出异常的类型,对应的捕获接收,如果没有对应的类型,则会被...接收
	如果没有...处理,则不会接收异常,程序会崩溃
	异常是按照最近的catch块接收,如果接收不住,则会向外继续抛出异常,由外面来接收
	*/
	catch(int n)//捕获抛出的整型
	{
		cout<<"int n = "<<n<<endl;
	}
	//不知道啥类型
	catch(...)
	{
		cout<<"error"<<endl;
	}
}
int main()
{
	fn(4,0);
}

自异常情况

对于代码

	try
	{
		char* p = nullptr;
		*p = 123;//error
	}

在这里插入图片描述
没有运行结果,出错了
在这里插入图片描述
进行处理

void fn(int x,int y)
{
	try
	{
		/*
		int* p = nullptr;
		*p = 123;//error
		*/
		int *p = nullptr;
		if(p == nullptr)
			throw "p is nullptr";
		*p = 123;
	}

	catch(float n)//捕获抛出的整型
	{
		cout<<"int n = "<<n<<endl;
	}
	catch(const char* str)
	{
		cout << "int n= "<< n << endl;
	}
	//不知道啥类型
	catch(...)
	{
		cout<<"error"<<endl;
	}
}
int main()
{
		fn(4,0);
}

在这里插入图片描述

异常是按照最近的catch块接收,如果接收不住,则会向外继续抛出异常,由外面来接收

void fn(int x,int y)
{
	/*在程序执行到此处时,如果为0,会出现未被处理的异常,在运行时出现*/
	//int t = x/y;//error
	try
	{
		if(y == 0)
			throw y;//抛出异常
		else
			cout<<x/y<<endl;
		char* p = nullptr;
		*p = 123;
	}
	/*
	按照抛出异常的类型,对应的捕获接收,如果没有对应的类型,则会被...接收
	如果没有...处理,则不会接收异常,程序会崩溃
	异常是按照最近的catch块接收,如果接收不住,则会向外继续抛出异常,由外面来接收
	*/
	catch(int n)//捕获抛出的整型
	{
		cout<<"int n = "<<n<<endl;
	}
	//不知道啥类型
	/*catch(...)
	{
		cout<<"error"<<endl;
	}*/
}
int main()
{	try
	{
		fn(4,0);
	}
	catch(...)
	{
		cout<<"error"<<endl;
	}
}

被外部主函数的catch接收了

在这里插入图片描述

3、 类型的判断问题

void fnn()
{
	try
	{
		throw 56.7;//double
		//throw 56.7f;//为float
	}
	catch(int n)
	{
		cout << "int n= " << n << endl;
	}
	catch(float b)
	{
		cout << "float b = " << b << endl;
	}
}
void main()
{
	fnn();
}

运行结果
可见throw 56.7;不是float和int 类型,应该为double类型
在这里插入图片描述
在这里插入图片描述

void fnn()
{
	try
	{
		throw 56.7;//double
		//throw 56.7f;//为float
	}
	catch(int n)
	{
		cout << "int n= " << n << endl;
	}
	catch(float b)
	{
		cout << "float b = " << b << endl;
	}
}
void main()
{	
	try
	{
		fnn();
	}
	catch(double dl)//用来捕获处理fnn函数中抛出的56.7的没有处理的异常
	{
		cout << "double dl = " << dl << endl;
	}
	catch(...)
	{
		cout << "..." << endl; 
	}
}

4、可以在catch中抛出异常,但不能在同一个函数中接收这个抛出的异常,只能在外部接收

void test()
{
	try
	{
		throw 1;
	}
	catch(int n)
	{
		cout << "int n = "<< n << endl;
		throw "hello";
	}
	catch(const char* str)//error,不能接收hello的抛出异常
	{
		cout << "str = "<< str << endl;
	}
}
void main()
{
	test();
}

在这里插入图片描述

但是可以被外部的接收

void test()
{
	try
	{
		throw 1;
	}
	catch(int n)
	{
		cout << "int n = "<< n << endl;
		throw "hello";
	}
	catch(const char* str)//error,不能接收hello的抛出异常
	{
		cout << "str = "<< str << endl;
	}
}
void main()
{
	test();
	
	catch(cosnt char* str)
	{
		cout << "str = " << str << endl;
	}
}

在这里插入图片描述
多个不同函数抛出异常

void test()
{
	try
	{
		throw 1;
	}
	catch(int n)
	{
		cout << "int n = "<< n << endl;
		throw "hello";
	}
	catch(const char* str)//error,不能接收hello的抛出异常
	{
		cout << "str = "<< str << endl;
	}
}
void test1()
{
	throw "test1";
}
void main()
{	
	try
	{
		test();
	}
	catch(cosnt char* str)
	{
		cout << "str = " << str << endl;
	}
	try
	{
		test1();
	}
	catch(...)
	{
		cout << "error" << endl; 
	}
}

在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ -- 异常:try、throw、catch 的相关文章

随机推荐