Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching boost serialisation archive exceptions

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?

like image 249
Infiltrator Avatar asked May 09 '11 16:05

Infiltrator


1 Answers

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.

like image 159
Fred Larson Avatar answered Sep 30 '22 05:09

Fred Larson