C++|异常处理类

2017-12-15  本文已影响0人  绍重先

http://www.runoob.com/cplusplus/cpp-exceptions-handling.html

摘自网络

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

#define TYPE_CLASS 0             //抛出自定义类类型对象的异常  
#define TYPE_INT 1                  //抛出整型的异常  
#define TYPE_ENUM 2             //抛出枚举的异常  
#define TYPE_FLOAT 3             //抛出float的异常  
#define TYPE_DOUBLE 4          //抛出double的异常  

typedef int TYPE;                    //异常的类型
enum Week {Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday};

//自定义的异常类
class MyException {
    public :
        MyException(string msg) {
            err_msg = msg;
        }
        void Reason() {
            cerr<<err_msg<<endl;
        }
        ~MyException() {}
    private:
        string err_msg;
};

//抛出异常的函数
//其中  throw (MyException,int,Week) 称之为异常规范,
//它告诉了编译器,该函数不会抛出其他类型的异常
//异常规范可以不写,默认为可以抛出任何类型的异常
//如果一个异常我们没有的捕获,会被系统调用terminate处理。
//如果一个异常类型,没有写入异常规范,我们使用catch无法捕获到,会被系统捕获,调用terminate
void KindsOfException(TYPE type) throw (MyException,int,Week,float,double) {
    switch(type) {
        case TYPE_CLASS:
            throw MyException("Exception! Type of Class"); //类
            break;
        case TYPE_INT:
            throw 2011;          //整型
            break;
        case TYPE_ENUM:
            throw Monday;     //枚举
            break;
        case TYPE_FLOAT:
            throw 1.23f;         //float
            break;
        case TYPE_DOUBLE:
            throw 1.23;          //double
            break;
        default:
            break;
    }
}

int main() {
    int type;
    cout<<"Input the type(0,1,2,3,4): ";
    cin>>type;
    try {
        KindsOfException(type);
    } catch(MyException e) { //如果我们使用了throw 异常规范,但是没把MyException写入throw列表,
        e.Reason();   //这里还是捕获不到MyException异常的,会被系统调用terminate处理。
    } catch (float f) {
        cerr<<"float"<<f<<endl;
    } catch (double d) {
        cerr<<"double"<<d<<endl;
    } catch(int i) {
        cerr<<"Exception! Type of Int -->"<<i<<endl;
    } catch(Week week) {
        cerr<<"Exception! Type of Enum -->"<<week<<endl;
    }
    //可以有更多的catch语句
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读