在函数列表后面描述抛出的异常.但是不起作用啊.为什么.我是在VC下编译的.
#include <exception>
#include <iostream>
#include <vector>
#include <string>
#include <exception>
using namespace std ;
class errStruct
{
public:
std::string strErr; errStruct()
{
strErr="Capture error!\n";
}
};void
ExceptionTest(void ) throw (errStruct)
{
cout <<"In the test function!\n";
throw new int;
}void
main(char *args,int argv)
{
std::cout<<"hello world!"<<endl; try{ ExceptionTest();
}
catch(errStruct d){ cout<<d.strErr;
}
catch(...){
cout<<"Some other errors\n";
}
}
运行结果:
hello world!
In the test function!
Some other errors
#include <exception>
#include <iostream>
#include <vector>
#include <string>
#include <exception>
using namespace std ;
class errStruct
{
public:
std::string strErr; errStruct()
{
strErr="Capture error!\n";
}
};void
ExceptionTest(void ) throw (errStruct)
{
cout <<"In the test function!\n";
throw new int;
}void
main(char *args,int argv)
{
std::cout<<"hello world!"<<endl; try{ ExceptionTest();
}
catch(errStruct d){ cout<<d.strErr;
}
catch(...){
cout<<"Some other errors\n";
}
}
运行结果:
hello world!
In the test function!
Some other errors
ExceptionTest(void ) throw (errStruct)
{
cout <<"In the test function!\n";
errStruct e;
throw e;
}
不过这样不安全,有浅拷贝,可以写拷贝构造函数或throw指针,不过要外部delete他。
#include <exception>
#include <iostream>
#include <vector>
#include <string>
#include <exception>
using namespace std ;
class errStruct
{
public:
std::string strErr; errStruct()
{
strErr="Capture error!\n";
} errStruct(errStruct &e)
{
strErr = e.strErr;
// other
}};void
ExceptionTest(void ) throw (errStruct)
{
cout <<"In the test function!\n";
errStruct e;
throw e;
}void
main(char *args,int argv)
{ std::cout<<"hello world!"<<endl; try{ ExceptionTest();
}
catch(errStruct d){ cout<<d.strErr;
}
catch(...){
cout<<"Some other errors\n";
}
}
#include <exception>
#include <iostream>
#include <vector>
#include <string>
#include <exception>
using namespace std ;
class errStruct
{
public:
std::string strErr; errStruct()
{
strErr="Capture error!\n";
}
};void
ExceptionTest(void ) throw (errStruct*)
{
cout <<"In the test function!\n";
throw new errStruct;
}void
main(char *args,int argv)
{ std::cout<<"hello world!"<<endl; try{ ExceptionTest();
}
catch(errStruct *d){ cout<<d->strErr;
delete d;
}
catch(...){
cout<<"Some other errors\n";
}
}