首页 新闻 会员 周边

我想问一个关于抛出异常类的问题

0
[待解决问题]
#include <iostream>
using namespace std;
class Example{
    public:
        Example(){
            cout<<"Example constructor !"<<endl;
        }
        ~Example(){
            cout<<"Example destructor !"<<endl;
        }
};
class ZeroException{
    public:
        ZeroException();
        ~ZeroException();
        ZeroException(ZeroException &ref);
        const char *getmsg(){
            return msg;
        }
    private:
        const char *msg;
};
ZeroException::ZeroException():msg("div zeero"){
    cout<<"ZeroException constructor !"<<endl;
}
ZeroException::ZeroException(ZeroException &ref){
    msg=ref.msg;
    cout<<"ZeroExeption cp constructor !"<<endl;
}
ZeroException::~ZeroException(){
    cout<<"ZeroException destructor !"<<endl;
}
int int_div(int n1,int n2){
    Example obj;
    if (n2==0){
        throw ZeroException();
    }
    else
        return n1/n2;
}
int main(){
    int int_n1,int_n2;
    while(1){
        cout<<"Please input two integers :";
        cin>>int_n1>>int_n2;
        try{
            cout<<"Maybe exception code :"<<endl;
            cout<<int_n1<<"/"<<int_n2<<"="<<int_div(int_n1,int_n2)<<endl;
            cout<<"int try,after div !"<<endl;
        } 
        catch(ZeroException divzero){
            cout<<"exception :"<<divzero.getmsg()<<endl;
        }
    }
    return 0;
}
问题补充:

vc++6.0运行就没有问题,dev-c++运行就有问题,这是咋整的

2017_06_25的主页 2017_06_25 | 菜鸟二级 | 园豆:202
提问于:2017-11-23 20:13
< >
分享
所有回答(1)
0

编译器的问题

fcyh | 园豆:568 (小虾三级) | 2017-11-24 14:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册