简体   繁体   English

这个C ++代码是什么意思:“ void Foo()throw;”?

[英]What does mean this C++ code: “void Foo() throw;”?

Question from the one interview. 来自一次采访的问题。

Please explain what does this C++ code mean: 请解释一下此C ++代码的含义:

void Foo() throw;
void Foo() throw;

This is a syntax error . 这是语法错误 The grammar for exception specification (C++98 §15.4) is: 异常规范(C ++ 98§15.4)的语法为:

exception-specification: 异常规范:

throw ( type-id-list opt ) throw ( type-id-list opt )

Note that the parenthesis are required. 请注意,括号是必需的。


On the other hand, 另一方面,

void Foo() throw();

means the function Foo() will not throw any exceptions. 表示函数Foo()不会引发任何异常。

void Foo() throw; is ill-formed. 格式不正确。

Instead void Foo() throw(); 而是void Foo() throw(); means that function 'Foo' is declared such that it does not throw any exception 表示函数“ Foo”已声明为不会引发任何异常

$15.4/10 - "A function with no exception-specification allows all exceptions. A function with an empty exceptionspecification, throw(), does not allow any exceptions." $ 15.4 / 10-“不具有异常规范的函数将允许所有异常。具有空异常规范的函数throw()将不允许任何异常。”

in an interview, this question provides a good opportunity to talk about exception checking; 在采访中,这个问题为讨论异常检查提供了一个很好的机会。 the differences between C++ and Java; C ++和Java之间的区别; your personal preferences and opinions; 您的个人偏好和意见; a time when exception checking has hurt or helped, etc. It's the kind of question that is not so much about the literally correct single sentence answer that states the syntax rule, but more about an invitation to talk about the way you program and think. 在这种情况下,异常检查会损害或有所帮助等。这种问题与其说是陈述语法规则的字面上正确的单句答案,不如说是更多的问题,而是更多地邀请您讨论编程和思考的方式。 Could be dangerous if you pretend to know more than you do, or if your philosophy is very different from theirs. 如果您假装比自己了解更多信息,或者您的哲学与他们的哲学截然不同,可能会很危险。

这意味着开发人员没有使用有意义的函数名...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM