上嵌学习笔记

C++基础-(异常)

2016-11-15  本文已影响15人  I踏雪寻梅

C++基础

异常

#include <iostream>
using namespace std;
int main()
{
    int n=1;
    try
    {
        int *p=new int;
        cout<<"begin"<<endl;
        if(n==1)
            throw 1;
        cout<<"after"<<endl;
    }
    catch(int)
    {
        cout<<"catch"<<endl;
        cout<<"end"<<endl;
    }
}
/*
begin
catch
end
*/
#include <iostream>
using namespace std;
class test
{
    public:
        int m_z;
};
int main()
{
    int n=1;
    try
    {
        int *p=new int;
        cout<<"begin"<<endl;
        test t1;
        t1.m_z=100;
        if(n==1)
            throw t1;
        cout<<"after"<<endl;
    }
    catch(test t2)
    {
        cout<<"catch"<<t2.m_z<<endl;
    
    }
    cout<<"end"<<endl;
}
/*
begin
catch100
end
*/
#include <iostream>
using namespace std;
class test
{
    public:
        int m_z;
};
class testSon::public test
{
    public:
        int m_w;
}
int main()
{
    int n=1;
    try
    {
        try
        {
            cout<<"begin"<<endl;
            test t1;
            t1.m_z=100;
            if(n==1)
                throw t1;
            cout<<"after"<<endl;
        }   
        catch(int)
        {
            cout<<"int catch"<<endl;
        }
    }
    catch(test t2)
    {
        cout<<"catch"<<t2.m_z<<endl;
    }
    catch(...)
    {
        cout<<"..."<<endl;
    }
    cout<<"end"<<endl;
}
/*
begin
catch100
end
*/
//throw 123;
/*
begin
int catch
end

*/
//throw 'a'
/*
begin
...
end
*/
//testSon t1;throw t1;
/*
begin
catch100
end
*/可以抛子类,抓父类
上一篇 下一篇

猜你喜欢

热点阅读