void fun()
{
try
{
cout<<"除零try"<<endl;
int zero = 0;
int f=1/zero;
}
catch(int )
{
}
}
int main(int argc, char* argv[])
{
try //如果没有异常,则跳过后面的所有catch语句块
{
fun();
}
catch(...) //捕获所有异常,
{
cout<<"主函数调用";
//throw; 这个捕获没有处理,可以传给外层或者下一级进行处理。
}
}
【点评】内层函数,没有捕获到异常,则自动调用外层的catch捕获异常。另外throw可抛出类型或者参数,比如class a;throw a;
string s = "sss", throw s; 但是在catch中必须是相同的类型或引用才能接收。