c++ - Catch block 不想捕获抛出的异常

标签 c++ qt exception

我有一个代码:

void Engine::count(const std::set<boost::filesystem3::path>& files)
{
    for (auto e : files)
    {
        try
        {
            count_(e);
        }
        catch (const Bad_formatting& ) // Here I'm trying to catch this exception
        {//but debugger never stops on this line even though breakpoint is set
            throw;                     // and re-throw it
        }
    }
} 

然后就是这个count_函数:

void Engine::count_(const boost::filesystem3::path& file)
{
    // and somewhere along this lines I'm throwing Bad_Formatting: 

    if (something)
    {

    }
    else
    {
        throw Bad_formatting(file,"No end of multicomment found.");
    }
}

但是在抛出这个异常之后,我收到一个对话框,告诉我我的应用程序请求运行时以一种不寻常的方式终止...
从来没有异常(exception)。为什么?这两个 fnc 都是静态的这一事实与它有什么关系吗?或者我正在使用 Qt?
编辑:
这是调用计数的代码:

try
    {

            Engine::count(files);



    }
    catch (const Bad_formatting& e)
    {

        QMessageBox::warning(nullptr,"Bad Formatting",msg);
    }  
//// 



struct Bad_formatting : public std::runtime_error
{
private:
    boost::filesystem3::path file_name_;

public:
    Bad_formatting(boost::filesystem3::path file_name,
                   const char* msg):std::runtime_error(msg),
                                    file_name_(file_name)
    {

    }

    const boost::filesystem3::path& file_name()const
    {
        return file_name_;
    }
    ~Bad_formatting()throw()
    {/*eb*/}
};

最佳答案

从你给你看的代码

  1. 抛出异常;
  2. 捕获它;
  3. 重新抛出它;
  4. 再也不会染上它了。

第 4 项似乎是重要的部分。

关于c++ - Catch block 不想捕获抛出的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8779539/

相关文章:

c++ - 当两个共享库定义相同的符号时实际发生了什么?

c++ - X().Y(Z()) 和标准中的评估顺序

c++ - Qt禁用不响应超时?

android NDK fatal error : stdio. h : No such file or directory #include <stdio. h>

c++ - 不允许抛出异常时如何处理失败的构造函数

python - linux/python 中的异常渗透

c++ - 我可以像这样混合使用 pimpl 和 C 接口(interface)吗?

c++ - Visual Studio 2010 - 独立函数中的链接器错误

c++ - 发送固定长度的 header

java - 二叉搜索树,添加相同元素异常(exception)。