title: c++ boost serialization how to prevent crashes with improper files
body: <p>I'm using boosts serialization functions to serialize and save data to external files in my project and then to be able to read them in again. </p>
  
  <p>The issue I'm having is that if a file is read in (in error) not in the correct format then the program crashes (perhaps expectedly).</p>
  
  <p>How can I identify that the file is in the wrong format and interrupt the read in process before the program crashses?</p>
  
  <p>EDIT:</p>
  
  <p>I've tried a try catch construct in my read in function which looks like this</p>
  
  <pre><code>int read_binary(file_in_out* object){
  
      std::ifstream ifs((*object).file.c_str());
  
      try
      {
          boost::archive::binary_iarchive ia(ifs);
          ia &gt;&gt; (*object).content;
  
  
      }
      catch (boost::archive::archive_exception&amp; ex) 
      {
          std::cout &lt;&lt; "An exception occurred. Exception Nr. "  &lt;&lt; '\n';
          return 1;
      }
      catch (int e)
      {
          std::cout &lt;&lt; "An exception occurred. Exception Nr. " &lt;&lt; e &lt;&lt; '\n';
          return 1;
      }
  
      return 0;
  
  }
  </code></pre>
  
  <p>This catches an exception when the file is nothing to do with the structures that it is trying to read into it. However, when I use an out of date version it doesn't catch an exception and crashes on line 'ia >> (*object).content;' Any ideas?</p>