Redian新闻
>
请教:函数后面的 throw() 有意义么?
avatar
请教:函数后面的 throw() 有意义么?# Programming - 葵花宝典
t*2
1
新手请教:C++有一种功能,在函数后面写throw()表示这个函数不会throw任何异常。
请问各位高手你们觉得这种功能什么意义么?如果函数想throw异常又定义了throw()是
不是会compiler-time error?
avatar
P*e
2
ft
no error
in try block, who ever first, who throw first and then just to catch block

【在 t*********2 的大作中提到】
: 新手请教:C++有一种功能,在函数后面写throw()表示这个函数不会throw任何异常。
: 请问各位高手你们觉得这种功能什么意义么?如果函数想throw异常又定义了throw()是
: 不是会compiler-time error?

avatar
t*2
3
no, my case is:
An exception specification with an empty throw, as in
void MyFunction(int i) throw(){
}
tells the compiler that the function does not throw any exceptions.

【在 P********e 的大作中提到】
: ft
: no error
: in try block, who ever first, who throw first and then just to catch block

avatar
P*e
4
大牛都去吃饭了
Here it is:
void translate() throw(unknown_word,bad_grammar) { /* ... */ }
explicitly states that it will only throw exception objects whose types are
unknown_word or bad_grammar, or any type derived from unknown_word or bad_
grammar
void foo() throw(){}
which means foo() cannot throw any exception

【在 t*********2 的大作中提到】
: no, my case is:
: An exception specification with an empty throw, as in
: void MyFunction(int i) throw(){
: }
: tells the compiler that the function does not throw any exceptions.

avatar
t*t
5
你把最重要的一点漏掉了:
如果出现了意料之外的exception类型,则调用unexpected().

are

【在 P********e 的大作中提到】
: 大牛都去吃饭了
: Here it is:
: void translate() throw(unknown_word,bad_grammar) { /* ... */ }
: explicitly states that it will only throw exception objects whose types are
: unknown_word or bad_grammar, or any type derived from unknown_word or bad_
: grammar
: void foo() throw(){}
: which means foo() cannot throw any exception

avatar
P*e
6
he just wanna know, what does this mean
avatar
i*y
7
这个问题我也疑惑了很久,下面的代码会有编译错误吗?
void ff() throw(int)
{
throw "error" ;
}

are

【在 P********e 的大作中提到】
: 大牛都去吃饭了
: Here it is:
: void translate() throw(unknown_word,bad_grammar) { /* ... */ }
: explicitly states that it will only throw exception objects whose types are
: unknown_word or bad_grammar, or any type derived from unknown_word or bad_
: grammar
: void foo() throw(){}
: which means foo() cannot throw any exception

avatar
c*g
8
编译没错,按标准运行的时候会有unexception
实际情况是有的compile也不会强制specification,比如MS VC

【在 i*****y 的大作中提到】
: 这个问题我也疑惑了很久,下面的代码会有编译错误吗?
: void ff() throw(int)
: {
: throw "error" ;
: }
:
: are

avatar
T*B
9
Following declaration gives a message to the user of your class: my method
doesn't throw any exception. Don't bother to put a try/catch block around it
when you use it.
void A::foo() throw();
It's your responsibility not to throw an exception in the definition of your
method. Say
void A::foo() throw() {
throw (5); // you are doomed here.
}
The compiler won't give an error on above code. But you will get a run time
error (your program aborts) if you implemented it this way.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。