C ++:未捕获自定义异常

时间:2016-08-09 15:42:32

标签: c++ exception

我搜索了但我找不到答案的解决方案。 我的问题如下:我的一个库在检查类型时抛出ns::xml::C_Exception。在另一个库中,类读取XML文件以获取配置。在执行官中,我已经使用正确的类型获得了try-catch。但例外情况并非如此。

我检查了抛出:我按价值投掷并且:

try {
    throw ns::xml::C_Exception ("mesg");
}
catch (const ns::xml::C_Exception& ex)
{
    cout << ex.what () << endl;
}

作品。

如果我直接使用exec中的检查,这与上面相同,则捕获异常。但是,当C_Configuration抛出异常时,捕获不起作用(甚至catch (...)也不起作用。)

对于我使用的每个try-catch块,这是控制台输出:

terminate called after throwing an instance of 'ns::xml::C_Exception'
  what():  mesg

我还检查了所有使用的函数:它们都有throw (ns::xml::C_Exception)修饰符。

C_Exception直接从std :: exception继承。

为了进行测试,C_Configuration总是抛出异常:

bool
C_Configuration::load (string file)
{

    // Useless code here

    throw xml::C_Exception ("mesg");

    // Useless code here
}

// In the header file :
bool
C_Configuration::reload (void)
{ return load (_file); }

这是我的主要内容:

int
main (int argc, char** argv)
{
    try {
        C_Configuration c("test.xml");
        c.reload ();
    }
    catch (const std::exception& ex)
    {
        cout << ex.what () << endl;
    }
    catch (...)
    {
        cout << "Oops" << endl;
    }

    // Useless code here.
    return 0;
}

相关文件(我的GitHub仓库中有,所以你可以看到完整的资源):

谢谢你的帮助。

编辑:

编译器标志:

  • 对于exec:-std=c++11(加上包含)
  • 对于图书馆:-std=c++11 -fPIC(加上包含)

链接器标志:

  • 对于exec:只是库和库路径。
  • 对于图书馆:-shared
编辑:@ZanLynx表示问题来自两个库之间的不同类型。我将这些库合并为一个,但仍然没有捕获到异常。

0 个答案:

没有答案