C++异常学习

C语言传统的处理错误的方式

传统的错误处理机制:

  1. 终止程序,如assert,缺陷:用户难以接受。如发生内存错误,除0错误时就会终止程序。
  2. 返回错误码,缺陷:需要程序员自己去查找对应的错误。如系统的很多库的接口函数都是通
    过把错误码放到errno中,表示错误
    实际中C语言基本都是使用返回错误码的方式处理错误,部分情况下使用终止程序处理非常严重的
    错误。

C++异常概念

异常是一种处理错误的方式,当一个函数发现自己无法处理的错误时就可以抛出异常,让函数的
直接或间接的调用者处理这个错误。
throw: 当问题出现时,程序会抛出一个异常。这是通过使用 throw 关键字来完成的。
catch: 在您想要处理问题的地方,通过异常处理程序捕获异常.catch 关键字用于捕获异
常,可以有多个catch进行捕获。
try: try 块中的代码标识将被激活的特定异常,它后面通常跟着一个或多个 catch 块。
如果有一个块抛出一个异常,捕获异常的方法会使用 try 和 catch 关键字。try 块中放置可能抛
出异常的代码,try 块中的代码被称为保护代码。使用 try/catch 语句的语法如下所示:

try
{
  // 保护的标识代码
}catch( ExceptionName e1 )
{
  // catch 块
}catch( ExceptionName e2 )
{
  // catch 块
}catch( ExceptionName eN )
{
  // catch 块
}

异常的使用

3.1 异常的抛出和捕获
异常的抛出和匹配原则

  1. 异常是通过抛出对象而引发的,该对象的类型决定了应该激活哪个catch的处理代码。
  2. 被选中的处理代码是调用链中与该对象类型匹配且离抛出异常位置最近的那一个。
  3. 抛出异常对象后,会生成一个异常对象的拷贝,因为抛出的异常对象可能是一个临时对象,
    所以会生成一个拷贝对象,这个拷贝的临时对象会在被catch以后销毁。(这里的处理类似
    于函数的传值返回)
  4. catch(…)可以捕获任意类型的异常,问题是不知道异常错误是什么。
  5. 实际中抛出和捕获的匹配原则有个例外,并不都是类型完全匹配,可以抛出的派生类对象,
    使用基类捕获,这个在实际中非常实用,我们后面会详细讲解这个。

在函数调用链中异常栈展开匹配原则
6. 首先检查throw本身是否在try块内部,如果是再查找匹配的catch语句。如果有匹配的,则
调到catch的地方进行处理。
7. 没有匹配的catch则退出当前函数栈,继续在调用函数的栈中进行查找匹配的catch。
8. 如果到达main函数的栈,依旧没有匹配的,则终止程序。上述这个沿着调用链查找匹配的
catch子句的过程称为栈展开。所以实际中我们最后都要加一个catch(…)捕获任意类型的异
常,否则当有异常没捕获,程序就会直接终止。
9. 找到匹配的catch子句并处理以后,会继续沿着catch子句后面继续执行。
在这里插入图片描述
看代码

#include<iostream>
using namespace std;
int divs(int m, int n)
{
	if (n == 0)
	{
		throw"被除数不能为0";
	}
	return m / n;
}

void func1(int m ,int n )
{
	divs(m, n);
}

void func2(int m, int n)
{
	func1(m, n);
}

void func3(int m, int n)
{
	func2(m, n);
}

int main()
{

	try
	{
		int m, n;
		cin >> m >> n;
		func3(m, n);
	}
	catch (const char* s)
	{
		cout << s << endl;
	}
	catch (...)
	{
		cout << 11 << endl;
	}


	return 0;
}

在这里插入图片描述
很高兴,这里学到了一个新的知识点,之前学的太马虎了

#include<iostream>
#include<string>
using namespace std;
int divs(int m, int n)
{
	if (n == 0)
	{
		throw"被除数不能为0";
	}
	return m / n;
}

void func1(int m ,int n )
{
	divs(m, n);
}

void func2(int m, int n)
{
	func1(m, n);
}

void func3(int m, int n)
{
	func2(m, n);
}

int main()
{

	try
	{
		int m, n;
		cin >> m >> n;
		func3(m, n);
	}
	catch (string s)
	{
		cout << s << endl;
	}
	catch (...)
	{
		cout << 11 << endl;
	}


	return 0;
}

用下面的代捕获就不行,就不知道原因,我throw不就是一个字符串吗,为什么不能接受。后来发现,throw抛出的是字符串常量,跟string类型确实不一样。我们创建一个string是怎么样的啊?
string s =“ssss”;
其实这里就是把字符串常量变成string类型。
下面是chat老师的解释

你的代码中使用 throw “被除数不能为0”; 时,抛出的异常是一个字符串常量,其类型是 const char* 而不是 std::string。因此,在 catch 块中,你应该捕获 const char* 类型的异常而不是 std::string。

如果你想要用 std::string 来捕获这个异常,你应该在抛出异常时使用 std::string 类型,像这样:throw std::string(“被除数不能为0”);。

字符串常量跟string有什么区别呢
ChatGPT
字符串常量和 std::string 类型在 C++ 中有几个主要的区别,这些区别涉及它们的类型定义、内存管理、使用方式和功能。这里是一些关键区别:

  1. 类型和存储
    字符串常量(如 “hello”)是由编译器在编译时处理的,它们实际上是 const char[] 类型,即一个以 null (‘\0’) 字符结束的字符数组。这些字符串存储在程序的只读数据段中,通常不能被修改。
    std::string 是 C++ 标准库中定义的一个类,用于处理文本数据。它封装了动态数组的管理,可以容纳可变长度的字符串,并提供了广泛的成员函数来进行字符串操作。std::string 对象通常存储在堆上,或者在小字符串优化 (SSO) 情况下在栈上。
  2. 内存管理
    字符串常量 不需要用户进行内存管理,它们由编译器管理,并且通常不能被修改。
    std::string 管理它自己的内存,自动进行内存分配和释放。它支持动态改变大小,可以在运行时添加、修改或删除字符。
  3. 功能和方法
    字符串常量 基本上只能用作不变的文本。要执行任何复杂的操作(如拼接、搜索或替换),你需要使用 C 风格的字符串处理函数(如 strlen, strcpy, strcat 等)。
    std::string 提供了许多成员函数和运算符重载,使得字符串处理更加直接和安全。例如,你可以使用 + 运算符来拼接字符串,使用 [] 运算符来访问特定位置的字符,以及使用 .length() 或 .size() 来获取字符串的长度等。
  4. 安全性和方便性
    使用 字符串常量 可能会涉及到指针操作,这在不正确使用的情况下可能导致程序崩溃或安全问题(例如缓冲区溢出)。
    std::string 设计时考虑到安全性和方便性,自动处理内存管理,减少了缓冲区溢出的风险,并简化了代码。

查看异常捕获机制

#define  _CRT_SECURE_NO_WARNINGS

#include<iostream>
using namespace std;
int divs(int m, int n)
{
	if (n == 0)
	{
		throw"被除数不能为0";
	}
	return m / n;
}

void func1(int m, int n)
{
	divs(m, n);
}

void func2(int m, int n)
{
	try
	{
		func1(m, n);
	}
	catch (const char* s)
	{
		cout << __LINE__ << endl;
	}
	cout << 1111 << endl;
	
}

void func3(int m, int n)
{
	func2(m, n);
}

int main()
{

	try
	{
		int m, n;
		cin >> m >> n;
		func3(m, n);
	}
	catch (const char* s)
	{
		cout << __LINE__ << endl;
		cout << s << endl;
	}
	catch (...)
	{
		cout << 11 << endl;
	}


	return 0;
}

在这里插入图片描述
自定义异常类
在这里插入图片描述

#define  _CRT_SECURE_NO_WARNINGS
#include<string>
#include <time.h>
using namespace std;

class Exception
{
public:
	Exception(const string& errmsg, int id)
		:_errmsg(errmsg)
		, _id(id)
	{}
	virtual string what() const
	{
		return _errmsg;	
	}
protected:
	string _errmsg;
	int _id;
};


class SqlException : public Exception
{
public:
	SqlException(const string& errmsg, int id, const string& sql)
		:Exception(errmsg, id)
		, _sql(sql)
	{}
	virtual string what() const
	{
		string str = "SqlException:";
		str += _errmsg;
		str += "->";
		str += _sql;
		return str;
	}
private:
	const string _sql;
};

class CacheException : public Exception
{
public:
	CacheException(const string& errmsg, int id)
		:Exception(errmsg, id)
	{}
	virtual string what() const
	{
		string str = "CacheException:";
		str += _errmsg;
		return str;
	}
};
class HttpServerException : public Exception
{
public:
	HttpServerException(const string& errmsg, int id, const string& type)
		:Exception(errmsg, id)
		, _type(type)
	{}
	virtual string what() const
	{
		string str = "HttpServerException:";
		str += _type;
		str += ":";
		str += _errmsg;
		return str;
	}
private:
	const string _type;
};

void HttpServer()
{
	// ...
	srand(time(0));
	if (rand() % 3 == 0)
	{
		throw HttpServerException("请求资源不存在", 100, "get");
	}
	else if (rand() % 4 == 0)
	{
		throw HttpServerException("权限不足", 101, "post");
	}
	CacheMgr();
}
void SQLMgr()
{
	srand(time(0));
	if (rand() % 7 == 0)
	{
		throw SqlException("权限不足", 100, "select * from name = '张三'");
	}
	//throw "xxxxxx";
}
void CacheMgr()
{
	srand(time(0));
	if (rand() % 5 == 0)
	{
		throw CacheException("权限不足", 100);
	}
	else if (rand() % 6 == 0)
	{
		throw CacheException("数据不存在", 101);
	}
	SQLMgr();
}

int main()
{
	while (1)
	{
		this_thread::sleep_for(chrono::seconds(1));
		try {
			HttpServer();
		}
		catch (const Exception& e) // 这里捕获父类对象就可以
		{
			// 多态
			std::cout << e.what() << std::endl;
		}
		catch (...)
		{
			std::cout << "Unkown Exception" << std::endl;
		}
	}
	return 0;
}

异常规范

  1. 异常规格说明的目的是为了让函数使用者知道该函数可能抛出的异常有哪些。 可以在函数的
    后面接throw(类型),列出这个函数可能抛掷的所有异常类型。
  2. 函数的后面接throw(),表示函数不抛异常。
  3. 若无异常接口声明,则此函数可以抛掷任何类型的异常。
// 这里表示这个函数会抛出A/B/C/D中的某种类型的异常
void fun() throw(A,B,C,D);
// 这里表示这个函数只会抛出bad_alloc的异常
void* operator new (std::size_t size) throw (std::bad_alloc);
// 这里表示这个函数不会抛出异常
void* operator delete (std::size_t size, void* ptr) throw();
// C++11 中新增的noexcept,表示不会抛异常
thread() noexcept;
thread (thread&& x) noexcept;
  • 25
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值