我有一个代码:
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*/}
};
最佳答案
从你给你看的代码
- 抛出异常;
- 捕获它;
- 重新抛出它;
- 再也不会染上它了。
第 4 项似乎是重要的部分。
关于c++ - Catch block 不想捕获抛出的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8779539/