#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
*/
異常與類的關(guān)系
#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
*/
進(jìn)化版
#include <iostream>
using namespace std;
class test
{
public:
int m_z;
};
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
*/
#include<iostream>
#include<vector>
using namespace std;
class zyz
{
public:
int m_z;
};
int main()
{
int n=1;
try
{
cout<<"begin"<<endl;
zyz z1;
z1.m_z=100;
if(n==1)
{
throw z1;
}
cout<<"after"<<endl;
}
catch(zyz z2)
{
cout<<"catch"<<z2.m_z<<endl;
}
cout<<"end"<<endl;
return 0;
}
//結(jié)果為:
//begin
//catch100
//end