传智扫地僧课程学习笔记。
学会使用标准异常库,
#include "iostream"
using namespace std;
#include <stdexcept>
#include "string"
class myexception : public exception
{
public:
myexception( const char *p)
{
this->m_p = p;
}
virtual const char *what()
{
cout<<"myexception: type"<<m_p<<endl;
return m_p;
}
private:
const char *m_p; //这里如果没有const,会报错,error C2440: “=”: 无法从“const char *”转换为“char *”
};
void testmyexception()
{
throw myexception("han shu yi chang");
}
void main()
{
try
{
testmyexception();
}
catch( myexception &e )
{
e.what();
}
catch( ... )
{
cout<<"unknow type"<<endl;
}
system("pause");
}