捕获 boost 序列化存档异常
我有以下情况。
这是一个代码片段,经过编辑以演示该问题。
ifstream ifs("empty_file");
if(!ifs.is_open()) {
ui.display("Error: Unable to open file \"empty_file\"\n");
return;
}
archive::text_iarchive ia(ifs);
try {
ia >> some_class;
} catch (...) {
ui.display("This should catch ever single exception\n");
}
empty_file确实是一个完全空的文件。这应该会导致错误;确实如此。问题是我无法捕获 boost 抛出的异常并滥用用户给我一个空/损坏的文件来加载。
相反,我得到以下信息:
Loading...
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): invalid signature
Aborted
据我所知,catch(...) 应该捕获所有存在的异常。我是否做错了什么,或者 boost::serialization 只是捕获了自己的异常并在我有机会做任何事情之前中止()?
如果是后者,那么是我的问题还是设计真的很糟糕?您的整个程序不应该仅仅因为损坏的加载文件而崩溃(abort()ing)。我能做什么呢?
I have the following situation.
This is a code snippet, edited to demonstrate the problem.
ifstream ifs("empty_file");
if(!ifs.is_open()) {
ui.display("Error: Unable to open file \"empty_file\"\n");
return;
}
archive::text_iarchive ia(ifs);
try {
ia >> some_class;
} catch (...) {
ui.display("This should catch ever single exception\n");
}
empty_file is indeed a completely empty file. This should cause an error; and it does. The problem is that I am unable to catch the exception thrown by boost and abuse the user about giving me an empty/corrupt file to load.
Instead, I get the following:
Loading...
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): invalid signature
Aborted
As far as I am aware, catch(...) should catch every exception in existence. Am I doing something wrong, or is boost::serialisation simply catching its own exception and abort()ing before I get the chance to do anything?
If the latter is the case, then is it just me or is that really bad design? Your entire program should not be crashing (abort()ing) just from a corrupt loading file. And what can I do about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许异常是由构造函数抛出的。尝试在
try
中构造您的 text_iarchive 对象,如下所示:假设捕获异常,您将需要捕获
boost::archive::archive_exception
而不是...
,当然。Maybe the exception is thrown by the constructor. Try constructing your text_iarchive object within the
try
, like this:Assuming that catches the exception, you'll want to catch the
boost::archive::archive_exception
instead of...
, of course.尝试检查链接选项。
我遇到了类似的问题,
catch(...)
因boost::property_tree
的write_ini()
中的异常而失败。我通过删除-static-libgcc
或将其与-static-libstdc++
一起使用来修复它。Try checking the link options.
I've got a similar problem,
catch(...)
failed on an exception inwrite_ini()
ofboost::property_tree
. I fixed it by removing-static-libgcc
, or using it with-static-libstdc++
together.