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;
}
相关推荐
prinrf('千寻)4 分钟前
MyBatis-Plus 的 updateById 方法不更新 null 值属性的问题
java·开发语言·mybatis
pystraf5 分钟前
LG P9844 [ICPC 2021 Nanjing R] Paimon Segment Tree Solution
数据结构·c++·算法·线段树·洛谷
m0_5557629014 分钟前
Qt缓动曲线详解
开发语言·qt
Funny-Boy21 分钟前
菱形继承原理
c++
飞川撸码1 小时前
【LeetCode 热题100】739:每日温度(详细解析)(Go语言版)
算法·leetcode·golang
揽你·入怀1 小时前
数据结构:ArrayList简单实现与常见操作实例详解
java·开发语言
yuhao__z1 小时前
代码随想录算法训练营第六十六天| 图论11—卡码网97. 小明逛公园,127. 骑士的攻击
算法
Echo``1 小时前
3:OpenCV—视频播放
图像处理·人工智能·opencv·算法·机器学习·视觉检测·音视频
AA-代码批发V哥2 小时前
Math工具类全面指南
java·开发语言·数学建模
Nobkins2 小时前
2021ICPC四川省赛个人补题ABDHKLM
开发语言·数据结构·c++·算法·图论