C++中try/catch/throw的使用

C++异常是指在程序运行时发生的反常行为,这些行为超出了函数正常功能的范围。当程序的某部分检测到一个它无法处理的问题时,需要用到异常处理。异常提供了一种转移程序控制权的方式。C++异常处理涉及到三个关键字:try、catch、throw。

在C++语言中,异常处理包括:

(1)、throw表达式:异常检测部分使用throw表达式来表示它遇到了无法处理的问题,throw引发了异常。throw表达式包含关键字throw和紧随其后的一个表达式,其中表达式的类型就是抛出的异常类型。throw表达式后面通常紧跟一个分号,从而构成一条表达式语句。

(2)、try语句块:异常处理部分使用try语句块处理异常。try语句块以关键字try开始,并以一个或多个catch子句结束。try语句块中代码抛出的异常通常会被某个catch子句处理。因为catch子句处理异常,所以它们也被称作异常处理代码。catch子句包括三部分:关键字catch、括号内一个(可能未命名的)对象的声明(称作异常声明,exception  declaration)以及一个块。当选中了某个catch子句处理异常之后,执行与之对应的块。catch一旦完成,程序跳转到try语句块最后一个catch子句之后的那条语句继续执行。try语句块声明的变量在块外部无法访问,特别是在catch子句内也无法访问。如果一段程序没有try语句块且发生了异常,系统会调用terminate函数并终止当前程序的执行。

(3)、一套异常类(exception class):用于在throw表达式和相关的catch子句之间传递异常的具体信息。

C++标准库定义了一组类,用于报告标准库函数遇到的问题。这些异常类也可以在用户编写的程序中使用,它们分别定义在4个头文件中:

(1)、exception头文件定义了最通常的异常类exception,它只报告异常的发生,不提供任何额外的信息。

(2)、stdexcept头文件定义了几种常用的异常类,如下(《C++ Primer(Fifth Edition)》):

(3)、new头文件定义了bad_alloc异常类型。

(4)、type_info头文件定义了bad_cast异常类型。

Exceptions provide a way to react to exceptional circumstances (like run time errors) in programs by transferring control to special functions called handlers.

To catch exceptions, a portion of code is placed under exception inspection. This is done by enclosing that portion of code in a try-block. When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.

An exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with the keyword catch, which must be placed immediately after the try block.

The C++ Standard library provides a base class specifically designed to declare objects to be thrown as exceptions. It is called std::exception and is defined in the<exception> header. This class has a virtual member function called what that returns a null-terminated character sequence (of type char *) and that can be overwritten in derived classes to contain some sort of description of the exception.

C++provides a list of standard exceptions defined in <exception> ( https://www.tutorialspoint.com/cplusplus/cpp_exceptions_handling.htm ):

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "try_catch.hpp"
#include <iostream>
#include <exception>
#include <vector>

//
// reference: http://www.cplusplus.com/doc/tutorial/exceptions/
int test_exception1()
{
	try {
		throw 20;
	}
	catch (int e) {
		std::cout << "An exception occurred. Exception Nr. " << e << '\n';
	}

	return 0;
}

class myexception : public std::exception
{
	virtual const char* what() const throw()
	{
		return "My exception happened";
	}
} myex;

int test_exception2()
{
	try {
		throw myex;
	}
	catch (std::exception& e) { // catches exception objects by reference (notice the ampersand & after the type)
		std::cout << e.what() << '\n';
	}

	return 0;
}

int test_exception3()
{
/*
	exception		description
	bad_alloc		thrown by new on allocation failure
	bad_cast		thrown by dynamic_cast when it fails in a dynamic cast
	bad_exception		thrown by certain dynamic exception specifiers
	bad_typeid		thrown by typeid
	bad_function_call	thrown by empty function objects
	bad_weak_ptr		thrown by shared_ptr when passed a bad weak_ptr
*/
	try {
		int* myarray = new int[1000];
	}
	catch (std::exception& e) { // Takes a reference to an 'exception' object
		std::cout << "Standard exception: " << e.what() << std::endl;
	}

	return 0;
}

///
// reference: http://en.cppreference.com/w/cpp/language/try_catch
int test_exception4()
{
	try {
		std::cout << "Throwing an integer exception...\n";
		throw 42;
	}
	catch (int i) {
		std::cout << " the integer exception was caught, with value: " << i << '\n';
	}

	try {
		std::cout << "Creating a vector of size 5... \n";
		std::vector<int> v(5);
		std::cout << "Accessing the 11th element of the vector...\n";
		std::cout << v.at(10); // vector::at() throws std::out_of_range
	}
	catch (const std::exception& e) { // caught by reference to base
		std::cout << " a standard exception was caught, with message '"
			<< e.what() << "'\n";
	}

	return 0;
}

/
// reference: https://www.tutorialspoint.com/cplusplus/cpp_exceptions_handling.htm
static int division(int a, int b) {
	if (b == 0) {
		throw "Division by zero condition!";
	}
	return (a / b);
}

int test_exception5()
{
	int x{ 50 }, y{ 0 }, z{ 0 };

	try {
		z = division(x, y);
		std::cout << z << std::endl;
	}
	catch (const char* msg) {
		std::cerr << msg << std::endl;
	}

	return 0;
}

struct MyException : public std::exception
{
	const char * what() const throw () {
		return "C++ Exception";
	}
};

int test_exception6()
{
	try {
		throw MyException();
	}
	catch (MyException& e) {
		std::cout << "MyException caught" << std::endl;
		std::cout << e.what() << std::endl;
	}
	catch (std::exception& e) {
		//Other errors
	}

	return 0;
}

int test_exception7()
{
	try {
		char* str = nullptr;
		str = new char[10];
		if (str == nullptr) throw "Allocation failure";
		for (int n = 0; n <= 100; n++) {
			if (n > 9) throw n;
			str[n] = 'z';
		}
	}
	catch (int i) {
		std::cout << "Exception: ";
		std::cout << "index " << i << " is out of range" << std::endl;
	}
	catch (char * str) {
		std::cout << "Exception: " << str << std::endl;
	}

	return 0;
}

GitHubhttps://github.com/fengbingchun/Messy_Test

  • 27
    点赞
  • 229
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值