mingw-gcc std::exception问题

来源:互联网 发布:汤臣倍健vb功效与作用 编辑:程序博客网 时间:2024/06/13 23:04

在MSVC中抛出异常代码如下:

#include <iostream>#include <stdexcept>#include <stdlib.h>void exception_test(){    throw std::exception("exception_test EXCEPTION");}
int main(){    try{        exception_test();    }    catch(const std::exception& e)    {        std::cout<<e.what()<<std::endl;    }    catch(...){        std::cout<<"exception catch...\n";    }    system("pause");    return 0;}

在main函数中可以正确的捕捉到std::exception异常,输出:
> exception_test EXCEPTION

但是在mingw-gcc,exception_tes()函数不能编译通过,

>  error: no matching function for call to 'std::exception::exception(const char [25])'

这是因为使用的编译器中std::exception根本就没有std::exception(const char* )类似的构造函数;


为了让exception在msvc和mingw-gcc中都能编译通过,修改exception_test()实现;

void exception_test(){    throw "exception_test STRING";    throw std::runtime_error("exception_test RUNTIME_ERROR");}
如果直接throw 字符串,main函数中输出

> exception catch...

如果 throw std::runtime_error,效果和在msvc中使用throw std::exception效果一样,

> exception_test RUNTIME_ERROR

原创粉丝点击