由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请教:函数后面的 throw() 有意义么?
相关主题
a C++ questionC++ func overload question
c++标准函数传递一问问一个windows下编译openGL code的问题
数组问题C++ class template specialization question
Question about a C++ compilation error on Visual Studio 2005问一个简单C++问题
引用的几个基本问题,有点糊涂c++中的inline 函数是做什么的?
请问c++为什么会编译失败?老年工程师转行学C++的更新的问题
关于C++中const的问题fatal error C1001: INTERNAL COMPILER ERROR
Intel C++ compiler 求教C++ Q19: throw 3
相关话题的讨论汇总
话题: throw话题: exception话题: 函数话题: void话题: foo
进入Programming版参与讨论
1 (共1页)
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.
1 (共1页)
进入Programming版参与讨论
相关主题
C++ Q19: throw 3引用的几个基本问题,有点糊涂
template 请问c++为什么会编译失败?
C array关于C++中const的问题
help!无法编译一个packageIntel C++ compiler 求教
a C++ questionC++ func overload question
c++标准函数传递一问问一个windows下编译openGL code的问题
数组问题C++ class template specialization question
Question about a C++ compilation error on Visual Studio 2005问一个简单C++问题
相关话题的讨论汇总
话题: throw话题: exception话题: 函数话题: void话题: foo