ITPub博客

首页 > 应用开发 > IT综合 > C++类和异常例子

C++类和异常例子

原创 IT综合 作者:gaopengtttt 时间:2016-08-08 11:53:21 0 删除 编辑
简单的列子:
#include
using namespace std;

class test
{
        private:
                int a;
                int b;
        public:
            test(int& a1,int& b1) {
                        a=a1;
                        b=b1;
                }
                void testcom(void){
                        if(a==b)
                        {
                                cout<<"exp hint!"<<endl;
                                throw *this;
                        }
                }
                void show(void)
                {
                        cout<<a<<" "<<b<<endl;=""  ="" }                 ~test(){};
};

int main(void)
{
        int a;
        int b;
        cin>>a;
        cin>>b;
        test t1(a,b);
        try{
                t1.testcom();
        }
        catch(test& t)
        {
                t.show();
                return 1;
        }
        return 0;
}

这里使用throw *this 来将具体的类数据返回给catch
</a<</endl;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7728585/viewspace-2123138/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: C++ new 和异常
请登录后发表评论 登录
全部评论
wxh gp_22389860 公众号 MySQL学习 书籍 深入理解MySQL主从原理

注册时间:2021-06-07

  • 博文量
    868
  • 访问量
    3495543