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;
}
相关推荐
学Linux的语莫13 分钟前
机器学习数据处理
java·算法·机器学习
csbysj202032 分钟前
如何使用 XML Schema
开发语言
R6bandito_37 分钟前
STM32中printf的重定向详解
开发语言·经验分享·stm32·单片机·嵌入式硬件·mcu
逆小舟43 分钟前
【C/C++】指针
c语言·c++·笔记·学习
earthzhang202144 分钟前
【1007】计算(a+b)×c的值
c语言·开发语言·数据结构·算法·青少年编程
江公望1 小时前
Qt QtConcurrent使用入门浅解
c++·qt·qml
杨枝甘露小码1 小时前
Python学习之基础篇
开发语言·python
我是华为OD~HR~栗栗呀1 小时前
23届考研-Java面经(华为OD)
java·c++·python·华为od·华为·面试
武文斌771 小时前
项目学习总结:LVGL图形参数动态变化、开发板的GDB调试、sqlite3移植、MQTT协议、心跳包
linux·开发语言·网络·arm开发·数据库·嵌入式硬件·学习
爱吃喵的鲤鱼1 小时前
仿mudou——Connection模块(连接管理)
linux·运维·服务器·开发语言·网络·c++