【发布时间】:2015-04-15 02:28:01
【问题描述】:
我在 Protocol Buffers 文档中没有找到任何关于 C++ 异常处理的内容。在 Javadoc 中明确定义了 InvalidProtocolBufferException,但在 C++ 中没有。
有时我运行我的程序,当它发现它认为有效消息中的缺失字段时它会崩溃,然后它会停止并抛出如下错误:
[libprotobuf FATAL google/protobuf/message_lite.cc:273] CHECK failed:
IsInitialized(): Can't serialize message of type "XXX" because it is
missing required fields: YY, ZZ
unknown file: Failure
C++ exception with description "CHECK failed: IsInitialized(): Can't
serialize message of type "XXX" because it is missing required fields:
YY, ZZ" thrown in the test body.
message_lite.cc 的source code 全部用“GOOGLE_DCHECK”或“InitializationErrorMessage”包裹...
我的应用程序不允许这样的异常来停止程序(不确定 C++ 中的术语是什么,但基本上没有 UncheckedExceptions),所以我真的需要一种方法来捕获这些异常,记录错误并优雅地返回,以防万一消息被严重损坏。反正有这样做吗?为什么我看到 this post 表示某种 google::protobuf::FatalException 但我找不到它周围的文档(只有 FatalException 可能也不够)。
谢谢!
【问题讨论】:
标签: c++ exception exception-handling protocol-buffers