C++学习日志48-----异常处理、bad_alloc、bad_cast、out_of_range


一、异常处理

#include<iostream>
using std::cout;
using std::endl;
using std::cin;

int main()
{
	int x{ 0 }, y{ 0 };
	cin >> x >> y;
	try
	{
		if (y == 0) throw y;
		cout << x / y;
	}
	catch (int& e)
	{
		cout << "the second number is :" << e << endl;
	}


}

在这里插入图片描述
结果如上图所示。

二、bad_alloc异常

#include<iostream>
#include<exception>
#include<stdexcept>
#include<new>

using std::cout;
using std::endl;

int main()
{
	try
	{
		for (int i = 0; i < 10000; i++)
		{
			auto* p = new long long int[700000];
			cout << i << " array" << endl;
		}
	}
	catch (std::bad_alloc& e)
	{
		cout << " exception:  " << e.what() << endl;
	}


}

在这里插入图片描述
结果如上图所示

三、bad_cast异常


#include<iostream>
#include<exception>
#include<stdexcept>
using std::cout;
using std::endl;
class Student
{
public:
	Student() = default;
	virtual void foo() {};
};
class Undergraduate :public Student {};
class Graduate : public Student{};

int main()
{
	Undergraduate u;
	Graduate g;
	Student* s1 = &u;
	Student* s2 = &g;
	Graduate* p = dynamic_cast<Graduate *>(s2);
	long x = reinterpret_cast<long>(p);
	cout << x << endl;
	Graduate* p2 = dynamic_cast<Graduate*>(s1);
	if (p2 == nullptr)
	{
		cout << "cast s1 to Graduate failed" << endl;
	}
	else
	{
		cout << " cast s1 to Graduate * succeeded" << endl;
	}
	try{
	Graduate& r1 = dynamic_cast<Graduate&>(u);
	}
	catch (std::bad_cast& e)
	{
		cout << "Exception " << e.what() << endl;
	}
}

在这里插入图片描述
结果如上图所示。

四、out_of_range异常


#include<iostream>
#include<vector>
using std::cout;
using std::endl;


int main()
{
	std::vector v{ 'a','b','c','d','e' };
	try
	{
		for (int i = 0; i <= 5; i++)
		{
			//  cout << v[i];  i=5越界
			cout << v.at(i) << endl; //i=会抛出异常
		}
	}
	catch (std::out_of_range& e)
	{
		cout << "Exception: " << e.what() << endl;
	}

}

在这里插入图片描述


结果如上图所示。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Microsoft C++ 异常 std::bad_alloc 是一种内存分配失败的异常。在 Visual C++ .NET 2002 中,标准 C++ 库中的 new 功能将支持 C++ 标准中指定的行为,如果内存分配失败,则会引发 std::bad_alloc 异常。同时,C 运行库的 new 函数也会引发相同的异常。 在你提供的引用中的代码中,异常捕获了 std::exception 类型的异常,并输出了异常信息。而在引用中,程序抓到了 std::bad_alloc 异常,这意味着系统无法分配所需的内存。 通常情况下,当系统内存不足时,可能会导致 std::bad_alloc 异常的发生。在你的例子中,通过查看任务管理器,发现内存已经占用了1.5G,这可能是导致内存分配失败的原因之一。 为了解决这个问题,你可以尝试释放一些内存资源,或者优化你的代码以减少内存的使用。另外,你还可以使用 try-catch 块来捕获并处理 std::bad_alloc 异常,以便在发生异常时采取相应的措施,比如显示错误信息或进行内存清理操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [详解C++中new运算符和delete运算符的使用](https://download.csdn.net/download/weixin_38571603/14874772)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [c++ std::bad_alloc异常问题排查](https://blog.csdn.net/sevendemage/article/details/126408227)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@白圭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值