cout 能抛出异常吗?
Can cout throw an exception?
- cout 是 "ostream"
类型的实例
-
If the operation sets an internal state flag that was registered with
member exceptions, the function throws an exception of member type failure.
这向我表明 cout 可以抛出异常。这是真的?什么样的情况会导致这种情况?
是的,但几乎没有人会调用 cout.exceptions(iostate)
来启用它。
编辑阐述:
std::ios_base
是一个抽象 class ,它为所有流提供基本的实用功能。 std::basic_ios<CharT, Traits>
是其中的一个抽象子class,添加了更多的实用函数(它又具有更多的子classes,导致人们实际实例化classes)。
std::ios_base::iostate
是一种位掩码类型,由以下位组成,可能 or
ed:
badbit (used for weird underlying errors)
eofbit (used after you hit EOF)
failbit (the normal error for badly-formatted input)
此外,iostate::goodbit
等同于 iostate()
(基本上是 0)。
通常,当您执行 I/O 时,您会在每次输入操作后检查流的布尔值以查看是否发生错误,例如if (cin >> val) { cout << val; }
...对于输出,可以简单地发出一堆并且只在最后检查成功(或者对于 cout,根本不检查)。
但是,有些人更喜欢异常,因此每个单独的流都可以配置为将其中一些 return 值转换为异常:
std::ios_base::iostate exceptions() const;
void exceptions(std::ios_base::iostate except);
在 C++ 中很少这样做,因为我们不会像其他一些语言的拥护者那样盲目地崇拜异常。特别是,“I/O 出了点问题”是 常见的 情况,因此扭曲控制流没有意义。
一个例子:
$ cat cout.cpp
#include <iostream>
int main()
{
std::cout.exceptions(std::cout.badbit);
std::cout << "error if written to a pipe" << std::endl;
}
$ sh -c 'trap "" PIPE; ./cout | true'
vvv 2018-06-21 23:33:13-0700
terminate called after throwing an instance of 'std::ios_base::failure[abi:cxx11]'
what(): basic_ios::clear: iostream error
Aborted
(请注意,在 Unix 系统上,您必须忽略 SIGPIPE 以便程序甚至有 机会 来处理此类错误,因为对于许多程序来说,只需退出是正确的做法 - 这通常是允许 head
工作的原因)
- cout 是 "ostream" 类型的实例
-
If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure.
这向我表明 cout 可以抛出异常。这是真的?什么样的情况会导致这种情况?
是的,但几乎没有人会调用 cout.exceptions(iostate)
来启用它。
编辑阐述:
std::ios_base
是一个抽象 class ,它为所有流提供基本的实用功能。 std::basic_ios<CharT, Traits>
是其中的一个抽象子class,添加了更多的实用函数(它又具有更多的子classes,导致人们实际实例化classes)。
std::ios_base::iostate
是一种位掩码类型,由以下位组成,可能 or
ed:
badbit (used for weird underlying errors)
eofbit (used after you hit EOF)
failbit (the normal error for badly-formatted input)
此外,iostate::goodbit
等同于 iostate()
(基本上是 0)。
通常,当您执行 I/O 时,您会在每次输入操作后检查流的布尔值以查看是否发生错误,例如if (cin >> val) { cout << val; }
...对于输出,可以简单地发出一堆并且只在最后检查成功(或者对于 cout,根本不检查)。
但是,有些人更喜欢异常,因此每个单独的流都可以配置为将其中一些 return 值转换为异常:
std::ios_base::iostate exceptions() const;
void exceptions(std::ios_base::iostate except);
在 C++ 中很少这样做,因为我们不会像其他一些语言的拥护者那样盲目地崇拜异常。特别是,“I/O 出了点问题”是 常见的 情况,因此扭曲控制流没有意义。
一个例子:
$ cat cout.cpp
#include <iostream>
int main()
{
std::cout.exceptions(std::cout.badbit);
std::cout << "error if written to a pipe" << std::endl;
}
$ sh -c 'trap "" PIPE; ./cout | true'
vvv 2018-06-21 23:33:13-0700
terminate called after throwing an instance of 'std::ios_base::failure[abi:cxx11]'
what(): basic_ios::clear: iostream error
Aborted
(请注意,在 Unix 系统上,您必须忽略 SIGPIPE 以便程序甚至有 机会 来处理此类错误,因为对于许多程序来说,只需退出是正确的做法 - 这通常是允许 head
工作的原因)