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;
}
相关推荐
闭着眼睛学算法13 分钟前
【双机位A卷】华为OD笔试之【模拟】双机位A-新学校选址【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·javascript·c++·python·算法·华为od
玉夏19 分钟前
【每日算法C#】爬楼梯问题 LeetCode
算法·leetcode·c#
卿摆摆23 分钟前
【C++】string的模拟实现
开发语言·c++
学好statistics和DS23 分钟前
【CV】泊松图像融合
算法·计算机视觉
贝塔实验室24 分钟前
QPSK信号载波同步技术---极性Costas 法载波同步
计算机网络·算法·网络安全·数学建模·信息与通信·信号处理·傅立叶分析
Dxy123931021626 分钟前
python如何使用nacos
开发语言·网络·python
前端小刘哥33 分钟前
视频直播点播平台EasyDSS视频直播功能的技术实现与应用场景解析
算法
前端小刘哥35 分钟前
企业运营效率瓶颈?新版视频直播点播平台EasyDSS用视频能力破局!
算法
曦樂~35 分钟前
一些可用于排序的函数(2542. 最大子序列的分数)
算法
玫瑰花店38 分钟前
C++速通Lambda表达式
开发语言·c++