问题及代码:
/*
*Copyright (c) 2015,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:吴胜男
*完成日期:2015年06月21日
*版本号:v1.0
*
*问题描述:补充阅读
*输入描述:
*程序输出:
*/
#include <iostream>
using namespace std;
void f();
class T
{
public:
T( )
{
cout<<"constructor"<<endl;
try
{
throw "<span style="color:#cc0000;">exception</span>";
}
catch(<span style="color:#cc0000;">char*</span>)
{
cout<<"exception"<<endl;
}
throw "exception";
}
~T( )
{
cout<<"destructor";
}
};
int main()
{
cout<<"main function"<< endl;
try
{
f( );
}
catch(<span style="color:#cc0000;">char *</span>)
{
cout<<"exception2"<<endl;
}
cout<<"main function"<<endl;
return 0;
}
void f( )
{
T t;
}
运行结果:
知识点总结:这个还没有搞明白,看了书来解决一下这个问题,在结构体中的throw "exception";是一个字符串型而上一级的catch为catch(char *)指针型,不匹配。再抛给上层,还是不匹配。为什么输出那一长串就真的不知道了。
学习心得:异常的处理多了还是有会绕晕。