C++,try示例,以及自定义错误类型结构体示例

参考:

C++中try/catch/throw的使用_c++try catch throw用法-CSDN博客

C++之 try语句块和异常处理_c++ try-CSDN博客

简单的try catch 示例:

cpp 复制代码
#include <iostream>
using namespace std;

double division(int a,int b)
{
	if(b==0)
	{
		throw "division by zero condition!";					// 第一个throw
	}
	return (a/b);
}

int main() {
	
	try{
		double a,b;
		//cin>>a>>b;
		a=1;
		b=0;
		cout<<division(a,b)<<endl;
	}
	catch(const char* e){			// 第一种
		cout<<"error:"<<e<<endl;
	}

	return 0;
}

自定义错误结构体:

cpp 复制代码
#include <iostream>
#include <exception>
#include <string>

using namespace std;

struct MyException : public exception{
	string err;
	MyException (){}
	MyException (const string s){
		err = s;
	}
	const char *what() const throw(){
		return (string("C++ MyException:") + err).c_str();
	}
};

double division(int a,int b)
{
	if(b==0)
	{
		//throw "division by zero condition!";					// 第一个throw
		throw MyException("division by zero condition!");		// 第二个thorw:自定义错误类型
	}
	return (a/b);
}

int main() {
	
	try{
		double a,b;
		//cin>>a>>b;
		a=1;
		b=0;
		cout<<division(a,b)<<endl;
	}
	catch(const char* e){			// 第一种
		cout<<"error:"<<e<<endl;
	}
	catch(MyException err){			// 第二种
		cout<<"myError:"<<err.what()<<endl;
	}
	

	return 0;
}

此处的const char *what() const throw() 后面的const throw(),意思是表示该函数不会修改对象的成员变量,且该函数不会抛出任何异常。 throw(),目前被noexcept关键字替代。

使用万能接收:

cpp 复制代码
#include <iostream>
#include <exception>
#include <string>

using namespace std;

struct MyException : public exception{
	string err;
	MyException (){}
	MyException (const string s){
		err = s;
	}
	const char *what() const throw(){
		return (string("C++ MyException:") + err).c_str();
	}
};

double division(int a,int b)
{
	if(b==0)
	{
		//throw "division by zero condition!";					// 第一个throw
		throw MyException("division by zero condition!");		// 第二个thorw:自定义错误类型
	}
	return (a/b);
}

int main() {
	
	try{
		double a,b;
		//cin>>a>>b;
		a=1;
		b=0;
		cout<<division(a,b)<<endl;
	}
	catch(const char* e){				// 第一种
		cout<<"error:"<<e<<endl;
	}
	// catch(MyException err){				// 第二种
	// 	cout<<"myError:"<<err.what()<<endl;
	// }
	catch (const std::exception& e) {   // 第三种:万能接收,也可以接收自定义错误
		std::cout <<"std error:"<< e.what() << '\n';
	}
	

	return 0;
}

使用标准报错输出:

cpp 复制代码
#include <iostream>
#include <exception>
#include <string>

using namespace std;

double division(int a,int b)
{
	if(b==0)
	{
		throw runtime_error("division by zero condition!");		// 第四个throw:使用标准报错
	}
	return (a/b);
}

int main() {
	
	try{
		double a,b;
		//cin>>a>>b;
		a=1;
		b=0;
		cout<<division(a,b)<<endl;
	}
	catch(runtime_error err){		 	   // 第四种:使用std标准错误类型
		cout<<"runtime_error:"<<err.what()<<endl;
	}
	

	return 0;
}
相关推荐
小飞猪Jay28 分钟前
C++面试速通宝典——13
jvm·c++·面试
Kalika0-040 分钟前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
_.Switch42 分钟前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
代码雕刻家1 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技1 小时前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web1 小时前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
龙图:会赢的1 小时前
[C语言]--编译和链接
c语言·开发语言
sp_fyf_20241 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-02
人工智能·神经网络·算法·计算机视觉·语言模型·自然语言处理·数据挖掘
rjszcb1 小时前
一文说完c++全部基础知识,IO流(二)
c++
小字节,大梦想2 小时前
【C++】二叉搜索树
数据结构·c++