t*********2 发帖数: 32 | 1 新手请教:C++有一种功能,在函数后面写throw()表示这个函数不会throw任何异常。
请问各位高手你们觉得这种功能什么意义么?如果函数想throw异常又定义了throw()是
不是会compiler-time error? |
P********e 发帖数: 2610 | 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?
|
t*********2 发帖数: 32 | 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
|
P********e 发帖数: 2610 | 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.
|
t****t 发帖数: 6806 | 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
|
P********e 发帖数: 2610 | 6 he just wanna know, what does this mean |
i*****y 发帖数: 1554 | 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
|
c*****g 发帖数: 119 | 8 编译没错,按标准运行的时候会有unexception
实际情况是有的compile也不会强制specification,比如MS VC
【在 i*****y 的大作中提到】 : 这个问题我也疑惑了很久,下面的代码会有编译错误吗? : void ff() throw(int) : { : throw "error" ; : } : : are
|
T***B 发帖数: 137 | 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. |