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
*/可以抛子类,抓父类