C++ 提升 io 流、错误处理
是否可以使自定义流在错误方面像标准流一样工作? 即默认使用好/失败/坏/eof 位而不是异常?
boost 文档仅提到抛出 std::failure 流错误并让其他错误传播(例如尝试分配缓冲区的 badalloc),但是 boost 代码似乎没有捕获这些,而是依赖用户代码来处理它们,但我所有现有的代码都依赖于 good()、bad() 等方法以及clear() 方法,以防出现错误后需要重试。
Is it possible to make a custom stream work like the stanadrd ones in regard for errors? That is by default use the good/fail/bad/eof bits rather than exceptions?
The boost docs only mention throwing an std::failure for stream errors and letting other error propagate (eg a badalloc from trying to allocate a buffer), however the boost code does not seem to catch these, instead relying on the user code to handle them, but all my existing code relies on the good(), bad() etc methods and the clear() method in cases where it needs to try again after an error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 http://www.trip.net/~bobwb/cppnotes/lec08.htm< /a>
可以使用以下命令设置错误状态:
voidclear(iostate = 0);
默认值零会导致 ios_base::goodbit 被设置。
清除();
因此相当于
clear(0);
相当于
clear(ios_base::goodbit);
请注意,ios_base::goodbit 是一个非零值。 clear() 可用于设置其他位之一,作为特定对象的运算符>>() 的程序员代码的一部分。 例如:
if (bad_char) is.clear(ios_base::badbit); // 设置istream的badbit
From http://www.trip.net/~bobwb/cppnotes/lec08.htm
The error state can be set using:
void clear(iostate = 0);
The default value of zero results in ios_base::goodbit being set.
clear();
is therefore equivalent to
clear(0);
which is equivalent to
clear(ios_base::goodbit);
Note that ios_base::goodbit is a non-zero value. clear() might be used to set one of the other bits as part of a programmer's code for operator>>() for a particular object. For example:
if (bad_char) is.clear(ios_base::badbit); // set istream's badbit