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;
}
相关推荐
程序员-King.2 小时前
【接口封装】——13、登录窗口的标题栏内容设置
c++·qt
学编程的小程3 小时前
LeetCode216
算法·深度优先
leeyayai_xixihah3 小时前
2.21力扣-回溯组合
算法·leetcode·职场和发展
01_3 小时前
力扣hot100——相交,回文链表
算法·leetcode·链表·双指针
萌の鱼3 小时前
leetcode 2826. 将三个组排序
数据结构·c++·算法·leetcode
Buling_03 小时前
算法-哈希表篇08-四数之和
数据结构·算法·散列表
AllowM3 小时前
【LeetCode Hot100】除自身以外数组的乘积|左右乘积列表,Java实现!图解+代码,小白也能秒懂!
java·算法·leetcode
Biomamba生信基地3 小时前
两天入门R语言,周末开讲
开发语言·r语言·生信
RAN_PAND3 小时前
STL介绍1:vector、pair、string、queue、map
开发语言·c++·算法
Bio Coder3 小时前
R语言安装生物信息数据库包
开发语言·数据库·r语言