在函数列表后面描述抛出的异常.但是不起作用啊.为什么.我是在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

解决方案 »

  1.   

    void
    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";
    }
    }
      

  2.   

    跟踪一下,是否进了catch(errStruct d)
      

  3.   

    我的意思是,在异常描述中限制输出errStruct,而在代码中我输出了INT 按照书上说的是要进行报错误的,但是我的代码却正确的执行了.