量化交易之C++篇 - 异常基本处理、异常变量的生命周期、异常的多态使用、使用系统提供的标准异常、自己编写异常类

// 异常基本处理
#include <iostream>
#include <string>

using namespace std;

class MyException { // 自定义异常类

public:
	void printError() {
		cout << "自定义的异常" << endl;
	}

};

class Person {

public:
	Person(string name) {
		this->name = name;
		cout << "Person: " << this->name << " 构造函数" << endl;
	}

	~Person() {
		this->name = name;
		cout << "Person: " << this->name << " 析构函数" << endl;
	}

private:
	string name;
};

int myDevide(int a, int b) {

	if (0 == b) {
		//throw - 1; // 抛出 int 类型的异常;
		//throw 3.14; // 抛出 double 类型的异常;
		//throw 'a'; // 抛出 char 类型的异常;(这三句代码谁先抛出异常, 就处理谁)

		// 栈解旋: 从try开始 到 throw 抛出异常之前 所有栈上的对象 都会被释放; 这个过程被称为栈解旋。
		Person person1("roger");
		Person person2("rei ri");
		Person* person3 = new Person("xiangkusu"); // 堆区的对象, 就没有被释放

		throw MyException(); // 匿名对象 (相当于调用了一个无参构造函数)
		delete person3; // 因为 MyException() 异常已经抛出, 所以 person3 的析构函数不会调用
	}

	return a / b;
}

void test01() {

	int a = 10;
	int b = -10;

	try {
		myDevide(a, 0);
	}
	catch (int) { // 捕获 int 异常
		cout << "int 类型异常捕获" << endl;
	}
	catch (double) {
		throw; // 如果不想处理这个异常, 可以继续向上抛出
		cout << "double 类型异常捕获" << endl;
	}
	catch (MyException exception) {
		cout << "MyException 类型异常捕获" << endl;
		exception.printError();
		throw;
		
	}
	catch (...) { // 捕捉其它异常
		cout << "char 类型异常捕获" << endl;
	}
}

int main() {

	try {
		test01();
	}
	catch(double) {
		cout << "main double 类型异常捕获" << endl;
	}
	catch (...) {
		cout << "main other 类型异常捕获" << endl;
	}

	return EXIT_SUCCESS;
}
// 异常变量的生命周期
#include <iostream>

using namespace std;

class MyException {

public:
	MyException() {
		cout << "MyException 默认构造 函数" << endl;
	}

	MyException(const MyException& exception) {
		cout << "MyException 拷贝构造 函数" << endl;
	}

	~MyException() {
		cout << "MyException 析构函数" << endl;
	}
};


void doWork() {
	throw new MyException();
	//throw MyException();
}

void test01() {

	try {
		doWork();
	}
	catch(MyException *exception) {
		cout << "*exception 异常" << endl;

		delete exception; // 异常对象的指针变量, 需要手动释放;
	}
	catch (MyException &exception) {
		cout << "exception 异常" << endl;
	}
	catch (...) {
		cout << "... 异常" << endl;
	}
}


int main() {

	test01();

	return EXIT_SUCCESS;
}
// 异常的多态使用
#include <iostream>

using namespace std;

class BaseException {

public:
	virtual void printError() {
		cout << "BaseException printError" << endl;
	}

};

class NullPointerException: public BaseException {
	
public:
	virtual void printError() {
		cout << "NullPointerException printError" << endl;
	}

};

class OutOfRangeException: public BaseException {

public:
	virtual void printError() {
		cout << "OutOfRangeException printError" << endl;
	}

};


void doWork() {
	//throw NullPointerException();
	throw OutOfRangeException();
}


void test01() {

	try {
		doWork();
	}
	catch(BaseException &baseException) {
		baseException.printError();
	}
	catch (NullPointerException &nullPointerException) {
		nullPointerException.printError();
	}
	catch (OutOfRangeException &outOfRangeException) {
		outOfRangeException.printError();
	}
	catch (...) {
		cout << "... 异常" << endl;
	}
}


int main() {

	test01();

	return EXIT_SUCCESS;
}
// 使用系统提供的标准异常
#include <iostream>
#include <string>

#include <stdexcept> // 系统提供的标准异常 要包含头文件;

using namespace std;

class Person {

public:
	Person(string name, int age) {
		this->name = name;

		if (age < 0 || age > 200) {

			throw length_error("长度越界");

			//throw out_of_range("年龄越界了!");
		}
	}

	string name;
	int age;
};


void test01() {
	try {
		Person person("roger", 201);
	}
	catch(out_of_range& exception) {
		cout << "out_of_range exception: " << exception.what() << endl; // exception.what(): 抛出异常时的错误提示
	}
	catch (length_error& exception) {
		cout << "length_error exception: " << exception.what() << endl;
	}
	catch (...) {
		cout << "... exception" << endl;
	}
}


int main() {

	test01();

	return EXIT_SUCCESS;
}
// 自己编写异常类
#include <iostream>
#include <string>

using namespace std;

class MyOutOfRangeException : public exception { // exception: 系统提供的异常基类

public:

	MyOutOfRangeException(string errorInfo) {
		this->errorInfo = errorInfo;
		cout << "MyOutOfRangeException: 有参构造函数" << endl;
	}

	virtual ~MyOutOfRangeException()
	{
		cout << "~MyOutOfRangeException: 析构函数" << endl;
	}

	virtual char const* what() const
	{
		return (this->errorInfo).c_str(); // string 转 char*
	}

private:
	string errorInfo;
};

class Person {

public:
	Person(string name, int age) {
		this->name = name;

		if (age < 0 || age > 200) {
			throw MyOutOfRangeException("so old");
		}
	}
	string name;
	int age;
};

void test01() {
	try {
		Person person("roger", 201);
	}
	catch (MyOutOfRangeException& myOutOfRangeException) { // 自定义异常类
		cout << "MyOutOfRangeException exception: " << myOutOfRangeException.what() << endl;
	}
	catch (...) {
		cout << "... exception" << endl;
	}
}

int main() {

	test01();

	return EXIT_SUCCESS;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值