C++异常(十五)

32 篇文章 0 订阅
  1. 异常的基本语法
    1. C++异常的处理关键字
      1. try  throw  catch
    2. 可以出现异常的代码 放到 try块
    3. 利用throw抛出异常
    4. 利用catch捕获异常
    5. catch( 类型) 如果想捕获其他类型 catch(…)
    6. 如果捕获到的异常不想处理,而继续向上抛出,利用 throw
    7. 异常必须有函数进行处理,如果都不去处理,程序自动调用 terminate函数,中断掉
    8. 异常可以是自定义数据类型
  2. 栈解旋
    1. 从try代码块开始,到throw抛出异常之前,所有栈上的数据都会被释放掉,
    2. 释放的顺序和创建顺序相反的,这个过程我们称为栈解旋
  3. 异常的接口声明
    1. 在函数中 如果限定抛出异常的类型,可以用异常的接口声明
    2. 语法: void func()throw(int ,double)
    3. throw()代表 不允许抛出异常
  4. 异常变量的生命周期
    1.       //抛出的是 throw MyException();  catch (MyException e) 调用拷贝构造函数 效率低
    2.       //抛出的是 throw MyException();  catch (MyException &e)  只调用默认构造函数 效率高 推荐
    3.       //抛出的是 throw &MyException(); catch (MyException *e) 对象会提前释放掉,不能在非法操作
    4.       //抛出的是 new MyException();   catch (MyException *e) 只调用默认构造函数 自己要管理释放
  5. 异常的多态使用
    1. 提供基类异常类
      1. class BaseException
      2. 纯虚函数  virtual void printError() = 0;
    2. 子类空指针异常 和  越界异常 继承 BaseException
    3. 重写virtual void printError()
    4. 测试 利用父类引用指向子类对象
  6. 系统标准异常
    1. 引入头文件  #include <stdexcept>
    2. 抛出越界异常 throw out_of_range(“…”)
    3. 获取错误信息  catch( exception & e )     e.what();

实例一 

#include<iostream>
#include<string>
#include<stdexcept>
using namespace std;

class person {
private:
	string name;
	int age;
public:
	person() {
		cout << "person" << endl;
	}
	person(string name,int age):name(name),age(age) {
		if (age < 0 || age>150) {
			throw out_of_range("年龄有问题! ");
		}
	}
	virtual void printerror() {
		cout << "person print error" << endl;
	};
	~person() {
		cout << "~person" << endl;
	}
};

class son:public person{
public:
	son() {
		cout << "son" << endl;
	}
	void printerror() {
		cout << "son error" << endl;
	}
	~son() {
		cout << "~son" << endl;
	}
};

void fun()throw(int) {
	throw 1;
}

void test() {
	try {
		//throw 1;
		//throw person();
		//throw "abc";
		//string str = "1234567890";
		//throw str;
		//fun();
		//throw son();
		person p("21",999);
	}
	catch(int){
		cout << "int error" << endl;
	}
	catch (char) {
		cout << "char error" << endl;
	}
	catch (double) {
		cout << "double error" << endl;
		throw;
		//默认double
	}
	catch (string) {
		cout << "string error" << endl;
	}
	//catch (person p) {
	//	cout << "person error" << endl;
	//}
	catch (person &p) {
		cout << "person error" << endl;
		p.printerror();
	}
	catch (exception& e) {
		cout << e.what() << endl;
	}
	catch (...) {
		cout << "other error" << endl;
		throw ;
	}
}

int main() {
	try {
		test();
	}
	catch (...) {
		cout << "other error" << endl;
	}
	system("pause");
	return EXIT_SUCCESS;
}

实例 二

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<stdexcept>//2013Vs可以不用
#include<string>
class MyOut_of :public exception
{
public:
	MyOut_of(const char *errorinfo)
	{
		//const char*转换string
		this->m_Info = string(errorinfo);
	}
	MyOut_of(const string errorinfo)
	{
		this->m_Info = errorinfo;
	}
	const char *  what() const
	{
		//把string转换const char*
		return this->m_Info.c_str();
	}
public:
	string m_Info;
};
class Maker
{
public:
	Maker(int age)
	{
		if (age<0 || age>150)
		{
			//throw out_of_range("年龄不在范围内");
			throw MyOut_of("自己的异常类,年龄不在范围内");
		}
		else
		{
			this->age = age;
		}
	}
public:
	int age;
};

void test()
{
	try
	{
		Maker m(200);
	}
	catch (out_of_range &ex)
	{
		cout << ex.what() << endl;
	}
	catch (MyOut_of &ex)
	{
		cout << ex.what() << endl;
	}
}

int main()
{
	test();
	system("pause");
	return EXIT_SUCCESS;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值