|
序列化归档异常 |
unregistered_class
invalid_signature
unsupported_version
unsupported_class_version
pointer_conflict
incompatible_native_format
array_size_too_short
input_stream_error
output_stream_error
invalid_class_name
unregistered_class
multiple_code_instantiation
xml_archive_parsing_error
xml_archive_tag_mismatch
xml_archive_tag_name_error
boost::archive_exception
对象,应用程序可以捕获该对象。这些异常在文件 archive_exception.hpp 和 basic_xml_archive.hpp 中定义。
namespace boost {
namespace archive {
class archive_exception : public std::exception
{
public:
typedef enum {
unregistered_class, // attempt to serialize a pointer of
// an unregistered class
invalid_signature, // first line of archive does not contain
// expected string
unsupported_version, // archive created with library version subsequent
// to this one
pointer_conflict // an attempt has been made to directly serialize
// an object after having already serialized the same
// object through a pointer. Were this permitted,
// the archive load would result in the creation
// of an extraneous object.
incompatible_native_format, // attempt to read native binary format
// on incompatible platform
array_size_too_short, // array being loaded doesn't fit in array allocated
input_stream_error // error on stream input
invalid_class_name, // class name greater than the maximum permitted.
// most likely a corrupted archive or an attempt
// to insert virus via buffer overrun method.
unregistered_cast, // base - derived relationship not registered with
// void_cast_register
unsupported_class_version, // type saved with a version # greater than the
// one used by the program. This indicates that the program
// needs to be rebuilt.
multiple_code_instantiation, // code for implementing serialization for some
// type has been instantiated in more than one module.
output_stream_error // error on stream output
} exception_code;
exception_code code;
archive_exception(exception_code c) : code(c) {}
virtual const char *what( ) const throw();
};
class xml_archive_exception : public virtual archive_exception
{
public:
typedef enum {
xml_archive_parsing_error, // archive doesn't contain expected data
xml_archive_tag_mismatch, // start/end tag in archive doesn't match program
xml_archive_tag_name_error // tag name contains invalid characters
} exception_code;
xml_archive_exception(exception_code c){}
virtual const char *what( ) const throw();
};
} // archive
} // boost
unregistered_class
BOOST_ARCHIVE_CUSTOM_ARCHIVE_TYPES
宏添加到系统中。invalid_signature
unsupported_version
如果较旧的程序尝试读取格式已更改的较新归档,则会抛出此异常。
unsupported_class_version
pointer_conflict
template<class Archive>
void T::save(Archive &ar) const
{
const A * aptr = &a;
ar << aptr; // save an instance of object of class A through a pointer
...
ar << a; // save an instance of an object of class A
assert(aptr == &a); // this must be true
}
template<class Archive>
void T::load(Archive &ar)
{
A * aptr;
ar >> aptr; // create and initialize a new instance of class A
...
ar >> a; // restore state of on object of class A
assert(aptr == &a); // this won't be true
}
首先通过指针保存对象,然后直接保存。在按相同顺序重新加载时,我们首先创建一个新对象并在其中加载其数据。然后,我们将数据加载到另一个现有对象中。在保存时我们从一个对象开始,但在还原后我们有两个对象。在更真实的情况下,可能很难发现此错误。幸运的是,当创建归档时可以检测到这种情况。当发生这种情况时,会抛出此异常。incompatible_native_format
array_size_too_short
input_stream_error
output_stream_error
这包括尝试读取文件末尾的内容。文本文件需要在文件末尾终止换行符,该换行符将在调用存档析构函数时追加。确保在相同流上打开输入存档之前,会销毁该流上的输出存档。也就是说,要使用类似以下内容的内容,而不是
std::stringstream ss;
std::vector<V> v;
boost::archive::text_oarchive oa(ss);
oa << v;
boost::archive::text_iarchive ia(ss);
ia >> v;
使用
std::stringstream ss;
std::vector<V> v;
{
boost::archive::text_oarchive oa(ss);
oa << v;
}
{
boost::archive::text_iarchive ia(ss);
ia >> v;
}
另一种情况是传递未初始化的数据。通常,当传递未初始化的数据时,序列化库的行为是未定义的。如果可以检测到,它将在调试生成中调用断言。否则,根据存档的类型,它可能会顺利通过,也可能导致存档中包含意外数据。这进而可能导致抛出此异常。
invalid_class_name
unregistered_cast
multiple_code_instantiation
xml_archive_parsing_error
xml_archive_tag_mismatch
xml_archive_tag_name_error
© Copyright 罗伯特雷米 2002-2004。按 Boost 软件许可证版本 1.0 分发。(请参阅随附文件 LICENSE_1_0.txt 或复制到 https://boost.ac.cn/LICENSE_1_0.txt)