exceptions.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!--
  4. (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
  5. Use, modification and distribution is subject to the Boost Software
  6. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. -->
  9. <head>
  10. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  11. <link rel="stylesheet" type="text/css" href="../../../boost.css">
  12. <link rel="stylesheet" type="text/css" href="style.css">
  13. <title>Serialization - Archive Exceptions</title>
  14. </head>
  15. <body link="#0000ff" vlink="#800080">
  16. <table border="0" cellpadding="7" cellspacing="0" width="100%" summary="header">
  17. <tr>
  18. <td valign="top" width="300">
  19. <h3><a href="../../../index.htm"><img height="86" width="277" alt="C++ Boost" src="../../../boost.png" border="0"></a></h3>
  20. </td>
  21. <td valign="top">
  22. <h1 align="center">Serialization</h1>
  23. <h2 align="center">Archive Exceptions</h2>
  24. </td>
  25. </tr>
  26. </table>
  27. <hr>
  28. <dl class="page-index">
  29. <dt><a href="#unregistered_class"><code style="white-space: normal">unregistered_class</code></a>
  30. <dt><a href="#invalid_signature"><code style="white-space: normal">invalid_signature</code></a>
  31. <dt><a href="#unsupported_version"><code style="white-space: normal">unsupported_version</code></a>
  32. <dt><a href="#unsupported_class_version"><code style="white-space: normal">unsupported_class_version</code></a>
  33. <dt><a href="#pointer_conflict"><code style="white-space: normal">pointer_conflict</code></a>
  34. <dt><a href="#incompatible_native_format"><code style="white-space: normal">incompatible_native_format</code></a>
  35. <dt><a href="#array_size_too_short"><code style="white-space: normal">array_size_too_short</code></a>
  36. <dt><a href="#input_stream_error"><code style="white-space: normal">input_stream_error</code></a>
  37. <dt><a href="#output_stream_error"><code style="white-space: normal">output_stream_error</code></a>
  38. <dt><a href="#invalid_class_name"><code style="white-space: normal">invalid_class_name</code></a>
  39. <dt><a href="#unregistered_class"><code style="white-space: normal">unregistered_class</code></a>
  40. <dt><a href="#multiple_code_instantiation"><code style="white-space: normal">multiple_code_instantiation</code></a>
  41. <dt><a href="#xml_archive_parsing_error"><code style="white-space: normal">xml_archive_parsing_error</code></a>
  42. <dt><a href="#xml_archive_tag_mismatch"><code style="white-space: normal">xml_archive_tag_mismatch</code></a>
  43. <dt><a href="#xml_archive_tag_name_error"><code style="white-space: normal">xml_archive_tag_name_error</code></a>
  44. </dl>
  45. Archive operators can throw a <code style="white-space: normal">boost::archive_exception</code>
  46. object which can be caught by an application program. These exceptions are defined
  47. in the files <a target="archive_exception_hpp" href="../../../boost/archive/archive_exception.hpp">
  48. archive_exception.hpp</a>
  49. and <a target="basic_xml_archive_hpp" href="../../../boost/archive/basic_xml_archive.hpp">
  50. basic_xml_archive.hpp</a>.
  51. <pre><code>
  52. namespace boost {
  53. namespace archive {
  54. class archive_exception : public std::exception
  55. {
  56. public:
  57. typedef enum {
  58. unregistered_class, // attempt to serialize a pointer of
  59. // an unregistered class
  60. invalid_signature, // first line of archive does not contain
  61. // expected string
  62. unsupported_version, // archive created with library version subsequent
  63. // to this one
  64. pointer_conflict // an attempt has been made to directly serialize
  65. // an object after having already serialized the same
  66. // object through a pointer. Were this permitted,
  67. // the archive load would result in the creation
  68. // of an extraneous object.
  69. incompatible_native_format, // attempt to read native binary format
  70. // on incompatible platform
  71. array_size_too_short, // array being loaded doesn't fit in array allocated
  72. input_stream_error // error on stream input
  73. invalid_class_name, // class name greater than the maximum permitted.
  74. // most likely a corrupted archive or an attempt
  75. // to insert virus via buffer overrun method.
  76. unregistered_cast, // base - derived relationship not registered with
  77. // void_cast_register
  78. unsupported_class_version, // type saved with a version # greater than the
  79. // one used by the program. This indicates that the proggram
  80. // needs to be rebuilt.
  81. multiple_code_instantiation, // code for implementing serialization for some
  82. // type has been instantiated in more than one module.
  83. output_stream_error // error on stream output
  84. } exception_code;
  85. exception_code code;
  86. archive_exception(exception_code c) : code(c) {}
  87. virtual const char *what( ) const throw();
  88. };
  89. class xml_archive_exception : public virtual archive_exception
  90. {
  91. public:
  92. typedef enum {
  93. xml_archive_parsing_error, // archive doesn't contain expected data
  94. xml_archive_tag_mismatch, // start/end tag in archive doesn't match program
  95. xml_archive_tag_name_error // tag name contains invalid characters
  96. } exception_code;
  97. xml_archive_exception(exception_code c){}
  98. virtual const char *what( ) const throw();
  99. };
  100. } // archive
  101. } // boost
  102. </code></pre>
  103. <p>
  104. <h3><a name="unregistered_class"><code style="white-space: normal">unregistered_class</code></a></h3>
  105. An attempt has been made to serialize a polymorphic class through a pointer
  106. without either registering it or associating it with an export key. This can also occur
  107. when using a new archive whose class name has not been added to the system with the
  108. <code style="white-space: normal">BOOST_ARCHIVE_CUSTOM_ARCHIVE_TYPES</code> macro.
  109. <h3><a name="invalid_signature"><code style="white-space: normal">invalid_signature</code></a></h3>
  110. Archives are initiated with a known string. If this string is not found when
  111. the archive is opened, It is presumed that this file is not a valid archive and this
  112. exception is thrown.
  113. <h3><a name="unsupported_version"><code style="white-space: normal">unsupported_version</code></a></h3>
  114. This system records the current library version number to all archives created. Note that this is in
  115. no way related to version number of classes used by application programs. This refers
  116. to the version of the serialization system used to create the archive. Future versions
  117. of this serialization system will be able to identify archives created under a previous
  118. (i.e. this) system and alter the loading procedure accordingly. Hence, future enhancements
  119. to this serialization system should not obsolete any existing archive files. It is only
  120. necessary to increment this version number when the newer system creates archives
  121. incompatible in format with the current one.
  122. <p>Should it ever occur that an older program attempts to read newer archives whose
  123. format has changed, this exception is thrown.
  124. <h3><a name="unsupported_class_version"><code style="white-space: normal">unsupported_class_version</code></a></h3>
  125. An attempt has been made to load a class whose version has been incremented since the
  126. program was written. Suppose that a class has been assigned version number 3 and the program
  127. has been built and sent to third parties. Now suppose that the definition of that class
  128. has been altered, the version number has been incremented to 4 and new archives have been
  129. built. If one attempts to load these new archives with the original program, this
  130. exception will be thrown.
  131. <h3><a name="pointer_conflict"><code style="white-space: normal">pointer_conflict</code></a></h3>
  132. To understand what this exception means consider the following scenario
  133. <pre><code>
  134. template&lt;class Archive&gt;
  135. void T::save(Archive &ar) const
  136. {
  137. const A * aptr = &a;
  138. ar << aptr; // save an instance of object of class A through a pointer
  139. ...
  140. ar << a; // save an instance of an object of class A
  141. assert(aptr == &a); // this must be true
  142. }
  143. template&lt;class Archive&gt;
  144. void T::load(Archive &ar)
  145. {
  146. A * aptr;
  147. ar >> aptr; // create and initialize a new instance of class A
  148. ...
  149. ar >> a; // restore state of on object of class A
  150. assert(aptr == &a); // this won't be true
  151. }
  152. </pre></code>
  153. An object is saved first through a pointer then directly. Upon loading back
  154. in the same sequence, we first create an new object and load in its data. Then
  155. we load the data into another existing object. Where we started with one
  156. object during save, we have two objects after restore. In a more realistic
  157. situation, it could be very difficult to find this error. Fortunately,
  158. these situations can be detected when the archive is created. When
  159. this occurs, this exception is thrown.
  160. <h3><a name = "incompatible_native_format"><code style="white-space: normal">incompatible_native_format</code></a></h3>
  161. The library currently supports char text, wide char text and native binary
  162. archive files. At the beginning of every archive, a signature is written indicating
  163. the type of archive. This exception is thrown when an attempt is made to read
  164. an archive written in a different format.
  165. <h3><a name="array_size_too_short"><code style="white-space: normal">array_size_too_short</code></a></h3>
  166. An attempt has been made to read an array that is larger than the array size.
  167. This should only occur when the size of an array in code is reduced after an
  168. archive has already been created.
  169. <h3>
  170. <a name="input_stream_error"><code style="white-space: normal">input_stream_error</code></a>
  171. <br>
  172. <a name="output_stream_error"><code style="white-space: normal">output_stream_error</code></a>
  173. </h3>
  174. An error has occured during stream input or ouput. Aside from the common
  175. situations such as a corrupted or truncated input file, there are
  176. several less obvious ones that sometimes occur.
  177. <p>
  178. This includes
  179. an attempt to read past the end of the file. Text files need a terminating
  180. new line character at the end of the file which is appended when the
  181. archive destructor is invoked. Be sure that an output archive on a stream
  182. is destroyed before opening an input archive on that same stream. That is,
  183. rather than using something like:
  184. <pre><code>
  185. std::stringstream ss;
  186. std::vector&lt;V&gt; v;
  187. boost::archive::text_oarchive oa(ss);
  188. oa &lt;&lt; v;
  189. boost::archive::text_iarchive ia(ss);
  190. ia &gt;&gt; v;
  191. </code></pre>
  192. use
  193. <pre><code>
  194. std::stringstream ss;
  195. std::vector&lt;V&gt; v;
  196. {
  197. boost::archive::text_oarchive oa(ss);
  198. oa &lt;&lt; v;
  199. }
  200. {
  201. boost::archive::text_iarchive ia(ss);
  202. ia &gt;&gt; v;
  203. }
  204. </code></pre>
  205. <p>
  206. Another one is the passing of uninitialized data. In general, the behavior
  207. of the serialization library when passed uninitialized data is undefined.
  208. If it can be detected, it will invoke an assertion in debug builds.
  209. Otherwise, depending on the type of archive, it may pass through without
  210. incident or it may result in an archive with unexpected data in it.
  211. This, in turn, can result in the throwing of this exception.
  212. <h3><a name="invalid_class_name"><code style="white-space: normal">invalid_class_name</code></a></h3>
  213. Class name length greater than the maximum permitted. Most likely cause is a corrupted
  214. archive or an attempt to insert a virus via the buffer overrun method.
  215. <h3><a name="unregistered_cast"><code style="white-space: normal">unregistered_cast</code></a></h3>
  216. In order to support casting between pointers of base and derived classes
  217. at runtime, a collection of legitimate conversions is maintained by the system.
  218. Normally this collection is maintained without any explicit action
  219. on the part of the user of the library. However, there are special cases
  220. where this might have to be done explicitly and could be overlooked. This
  221. is described in <a href="serialization.html#runtimecasting">Runtime Casting</a>.
  222. This exception is thrown if an attempt is made to convert between two pointers
  223. whose relationship has not been registered,
  224. <h3><a name="multiple_code_instantiation"><code style="white-space: normal">multiple_code_instantiation</code></a></h3>
  225. This exception is thrown when it is detected that the serialization of the same type
  226. has been instantiated more than once. This might occur when
  227. serialization code is instantiated in both the mainline and one or more DLLS.
  228. <h3><a name="xml_archive_parsing_error"><code style="white-space: normal">xml_archive_parsing_error</code></a></h3>
  229. The XML generated by the serialization process is intimately coupled to the
  230. C++ class structure, relationships between objects and the serialization
  231. specifications. If these become out of sync in any way, the XML may not map
  232. to the loading serialization and this exception might be thrown. This might
  233. occur for one of the following reasons:
  234. <ul>
  235. <li>The archive has been edited outside the serialization system. This might
  236. be possible if only the data is changed and the XML attributes and nesting
  237. structure are left unaltered. But any other editing is likely to render the
  238. archive unreadable by the serialization library.
  239. <li>The serialization has been altered and an archive generated by the old
  240. code is being read. That is, versioning has not been properly employed to
  241. properly deserialize previously created archives.
  242. </ul>
  243. <h3><a name="xml_archive_tag_mismatch"><code style="white-space: normal">xml_archive_tag_mismatch</code></a></h3>
  244. This exception will be thrown if the start or end tag of an XML element doesn't match
  245. the name specified for the object in the program.
  246. <h3><a name="xml_archive_tag_name_error"><code style="white-space: normal">xml_archive_tag_name_error</code></a></h3>
  247. This exception will be thrown if the tag name contains invalid characters. Valid characters
  248. for an XML tag are: upper and lower case letters, digits, and the following punctuation: .(period),
  249. _(underscore), :(colon), and -(hyphen).
  250. <hr>
  251. <p><i>&copy; Copyright <a href="http://www.rrsd.com">Robert Ramey</a> 2002-2004.
  252. Distributed under the Boost Software License, Version 1.0. (See
  253. accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  254. </i></p>
  255. </body>
  256. </html>