捕获 boost 序列化存档异常

发布于 2024-11-06 04:01:55 字数 777 浏览 1 评论 0原文

我有以下情况。

这是一个代码片段,经过编辑以演示该问题。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的位置 2024-11-13 04:01:55

也许异常是由构造函数抛出的。尝试在 try 中构造您的 text_iarchive 对象,如下所示:

try {
    archive::text_iarchive ia(ifs);
    ia >> some_class;
} catch (...) {
    ui.display("This should catch every single exception\n");
}

假设捕获异常,您将需要捕获 boost::archive::archive_exception 而不是 ...,当然。

Maybe the exception is thrown by the constructor. Try constructing your text_iarchive object within the try, like this:

try {
    archive::text_iarchive ia(ifs);
    ia >> some_class;
} catch (...) {
    ui.display("This should catch every single exception\n");
}

Assuming that catches the exception, you'll want to catch the boost::archive::archive_exception instead of ..., of course.

江湖彼岸 2024-11-13 04:01:55

尝试检查链接选项。

我遇到了类似的问题,catch(...)boost::property_treewrite_ini() 中的异常而失败。我通过删除 -static-libgcc 或将其与 -static-libstdc++ 一起使用来修复它。

Try checking the link options.

I've got a similar problem, catch(...) failed on an exception in write_ini() of boost::property_tree. I fixed it by removing -static-libgcc, or using it with -static-libstdc++ together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文