d_12_2.cpp

来自「C++应用教程原码,里面包含该书中有十三章内容的代码,详细具体」· C++ 代码 · 共 55 行

CPP
55
字号
#include "stdafx.h"
#include <iostream>
#include <string>
#include<iomanip>
using namespace std;

void function(void);
class overflow
{
public:
	overflow(){}
	~overflow(){}
	const char* ShowReason() const
	{
		return "这是overflow类异常,现在进行处理......";
	}
};
class A
{
public:
	A();
	~A();
};
A::A()
{
	cout<<"构造类A。"<<endl;
}
A::~A()
{
	cout<<"析构类A。"<<endl;
}
void function()
{
	A D;
	cout<<"在函数function中抛掷出overflow类异常。"<<endl;
	throw overflow();
}
void main()
{
	cout<<"在main函数中"<<endl;
	try
	{
		cout<<"在try块中调用function()。"<<endl;
		function();
	}
	catch(overflow E)
	{
		cout<<"捕获到overflow类型异常,进入catch异常处理程序。"<<endl;
		cout<<E.ShowReason()<<endl;
	}

	cout<<"异常处理完毕,返回main函数。"<<endl;
    cin.get(); //等待结束,以便调测程序,可以删除
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?