异常总是指定。 如果foo()没有明确的异常规范,那么它可以抛出任何异常。 void foo() {} //抛出(任何东西) 然而, foo()throws(){} 然后合约说foo不会抛出异常。 如果程序违反合约C ++调用terminate()。 在你的例子中,如果foo除了char之外还抛出了其他东西,则终止() 被调用。Exceptions are always specified.If foo() did not have an explicit exception specification then itcould throw any kind of exception.void foo() { } // throws(anything)however,foo() throws() { }then the contract says foo shall throw no exception.If the program breaks the contract C++ calls terminate().In your example, if foo threw anything else but a char, terminate()gets called. 和一个小的挑剔它是无效的foo()throw(char&),而不是* throws *。 我可以在常见问题解答中发誓,但我找不到;在 除了nothrow之外一般使用异常规范 [void foo()throw(){}]是不鼓励的,因为它没有强制执行 编译时间(参见Java)。And a minor nitpick it''s void foo() throw(char&), not *throws*.And I could have sworn it''s in the FAQ, but I can''t find it; ingeneral use of exception specifications with the exception of nothrow[void foo() throw() { }] is discouraged because it doesn''t enforce atcompile time (cf. Java). 这篇关于声明异常的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-27 20:05
查看更多