您的位置:首页 > 编程语言 > C语言/C++

C++实现h5牌九棋牌平台异常的几种捕获方式

2018-02-13 13:41 225 查看

捕获指定的类型

这样的话可以对每种异常做出不同的处理,例如:
#include <iostream>

using namespace std;

void A(int n){
int a = 1;
float b = 0.2;
double c = 0.3;

if(n == 1)
throw a;
else if(n == 2)
throw b;
else if(n == 3)
throw c;
}

int main()
{
int test;

while(cin >> test){
try{
A(test);
}catch(int i){
cout << "Catch int exception " << i << endl;
}catch(float i){
cout << "Catch float exception " << i << endl;
}catch(double i){
cout << "Catch double exception " << i << endl;
}
}

return 0;
}

捕获泛型

如果想捕获全部类型异常的话,C++ 提供了一种简便的做法,在 catch 子句的异常声明中使用省略号来作为异常声明,例如:
void function(){
try {
/* your code */
}catch(...) {
/* handler */
}
}

捕获类

例如:
#include <iostream>

using namespace std;

class Base{
public:
void name(){
cout << "My name is Base" << endl;
}
};

void A(){
throw Base();
}

int main()
{
try{
A();
}catch(Base &e){
e.name();
}

return 0;
}
也可以捕获 Base 的子类,并且在 Base 类的成员函数前加 virtual 实现多态,这样的话就可以调用子类的 name 方法,例如:
#include <iostream>

using namespace std;

class Base{
public:
virtual void name(){
cout << "My name is Base" << endl;
}
};

class Derived : public Base{
public:
void name(){
cout << "My name is Derived" << endl;
}
};

void A(){
throw Derived();
}

int main()
{
try{
A();
}catch(Base &e){
e.name();
}

return 0;
}

捕获未期望的异常

可以在函数名后用 throw 来声明该函数可能抛出的异常,例如:
#include <iostream>

using namespace std;

void A() throw (int, float)
{
int a = 1;
float b = 0.2;
double c = 0.3;

throw c;
}

int main()
{
try{
A();
}catch(...){
cout << "Catch exception" << endl;
}

return 0;
}
但是,如果函数抛出的异常类型未在声明范围内的话,程序就会发生崩溃:运行结果:
terminate called after throwing an instance of 'double'
Aborted (core dumped)
即使你使用了 catch(...) ,但它也捕获不到,异常会继续向上汇报,最终被系统默认的函数进行处理。h5牌九棋牌平台出售(h5.hxforum.com) 联系方式170618633533企鹅2952777280 微信Tel17061863533 源码出售,售房卡出售,平台出租但我们可以使用 set_unexpected (unexpected_handler func) 这个函数来修改默认的处理方法,来实现自己的处理方式。

未实现捕获的异常

假如函数抛出一个 double 的异常,但在我们捕获的函数中没有实现 double 类型的捕获,当然也没有使用 catch(...),这种情况和未期望的异常差不多,它也会上报系统,调用系统默认的处理函数。同样我们也可以更改这个默认函数,使用如下方法:
terminate_handler set_terminate (terminate_handler func)
示例程序:
#include <iostream>

void exception(){
std::cout << "my_terminate" << std::endl;
}

int main()
{
std::set_terminate(exception);

throw 0;

return 0;
}
运行结果:
my_terminate
Aborted (core dumped)
你的支持就是我的动力!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: