C++ try catch 的使用

本文详细介绍了在C++中如何使用异常处理来增强代码的健壮性和错误处理能力。通过两个具体的示例,展示了在函数内部和调用子函数时如何抛出和捕获异常,确保程序在遇到错误时能够优雅地进行错误提示并继续执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、在当前函数中简单直接使用


	for (int i = 0; i < 5; i++){
		std::vector<int> temp;
		if (i != 3){
			temp.push_back(i);
		}
		try{
			if (temp.size() == 0){
				throw "temp is empty!\n";
			}
				
		}
		catch (char* e){
			
				printf(e);
				continue;			
		}
		std::cout << temp[0] << std::endl;
	}

2、在某一个函数中处理调用的子函数的异常

int  add(std::vector<int> &tmp, int a){
	if (tmp.size() == 0)
		throw "tmp is empty!\n";
	return tmp[0] + a;
}

int _tmain(int argc, _TCHAR* argv[])
{	
	for (int i = 0; i < 5; i++){
		std::vector<int> temp;
		int b = 0;
		if (i != 3){
			temp.push_back(i);
		}
		try{
			b=add(temp, i);				
		}
		catch (char* e){			
				printf(e);
				continue;			
		}
		std::cout << temp[0]<<" "<<i<<" "<<b << std::endl;
	}

	return 0;
}

 

### C++ 中 `try-catch` 的用法与示例 在 C++ 中,`try-catch` 是一种异常处理机制,用于捕获和处理程序运行时可能出现的错误。通过将可能抛出异常的代码块放入 `try` 块中,并在 `catch` 块中定义如何处理这些异常,可以有效增强程序的健壮性[^1]。 以下是一个完整的 `try-catch` 示例: ```cpp #include <iostream> #include <stdexcept> // std::runtime_error void testFunction() { throw std::runtime_error("An error occurred"); } int main() { try { testFunction(); // 可能抛出异常的函数 } catch (const std::exception& e) { // 捕获标准异常 std::cout << "Caught exception: " << e.what() << std::endl; } return 0; } ``` 在此示例中: - 函数 `testFunction` 抛出了一个 `std::runtime_error` 异常。 - 在 `main` 函数中,`try` 块调用了 `testFunction`,如果发生异常,则进入 `catch` 块。 - `catch` 块捕获了 `std::exception` 类型的异常,并输出异常信息[^1]。 #### 自定义异常类的使用 除了捕获标准库提供的异常类型外,还可以定义自己的异常类。例如: ```cpp #include <iostream> #include <exception> class MyException : public std::exception { public: const char* what() const noexcept override { return "My custom exception occurred"; } }; int main() { try { throw MyException(); // 抛出自定义异常 } catch (const std::exception& e) { std::cout << "Caught exception: " << e.what() << std::endl; } return 0; } ``` 在这个例子中,`MyException` 继承自 `std::exception`,并重写了 `what()` 方法以返回自定义的异常信息[^2]。 #### 捕获所有类型的异常 如果需要捕获所有类型的异常而不仅仅是特定类型,可以使用通用的 `catch(...)`: ```cpp #include <iostream> int main() { try { throw 42; // 抛出一个整数异常 } catch (...) { // 捕获所有类型的异常 std::cout << "An unknown exception was caught" << std::endl; } return 0; } ``` 此代码段展示了如何使用 `catch(...)` 来捕获任何未指定类型的异常[^4]。 #### 异常处理顺序 当有多个 `catch` 块时,它们会按照定义的顺序依次匹配。例如: ```cpp #include <iostream> int main() { try { throw 42; // 抛出一个整数异常 } catch (int i) { // 匹配整数类型的异常 std::cout << "The integer exception was caught, with value: " << i << std::endl; } catch (...) { // 捕获其他所有类型的异常 std::cout << "A different type of exception was caught" << std::endl; } return 0; } ``` 在这个例子中,由于抛出的是整数异常,因此第一个 `catch` 块会被触发[^5]。 ### 注意事项 - 如果 `catch` 块没有正确匹配到异常类型,则会跳过该 `catch` 块,继续寻找后续的匹配项。 - 使用 `catch(...)` 可以确保不会遗漏任何异常,但通常建议优先捕获已知类型的异常。 - 在多层函数调用中,如果某个函数内部未处理异常,则异常会逐层向外传播,直到被捕获或导致程序终止[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值