声明异常的函数 [英] function with exception's declared

查看:80
本文介绍了声明异常的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用函数声明的优点和缺点

指定了异常。

像foo()throws(char&)

What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)

推荐答案

2008-09-15 11:54,raj s写道:
On 2008-09-15 11:54, raj s wrote:

什么是优点和缺点使用函数声明

指定例外。

如foo()throws(char&)
What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)



如果你有一个带有异常规范的函数你可以工作

假设函数只抛出

指定种类的异常。换句话说,你不需要在你的处理程序中检查其他类型的

异常。


缺点是如果规范中没有列出异常是

抛出函数(或由函数调用的函数)

也没有被函数捕获,你的程序会调用unexpected()

通常会终止你的申请。


-

Erik Wikstr ?? m

If you have a function with an exception specification you can work
under the assumption that the function only throws exceptions of the
specified kind. In other words you do not have to check for other kinds
of exceptions in your handlers.

The drawback is that if an exception not listed in the specification is
thrown in the function (or by a function called by the function) which
is not also caught in the function, your program will call unexpected()
which will usually terminate your application.

--
Erik Wikstr??m

9月15日上午5:54,raj s< yesr ... @ gmail.comwrote:
On Sep 15, 5:54 am, raj s <yesr...@gmail.comwrote:

使用的优点和缺点是什么函数声明

指定了异常。

像foo()throws(char&)
What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)



异常总是指定。

如果foo()没有明确的异常规范,那么它可以抛出任何异常。

void foo() {} //抛出(任何东西)


然而,

foo()throws(){}

然后合约说foo不会抛出异常。

如果程序违反合约C ++调用terminate()。


在你的例子中,如果foo除了char之外还抛出了其他东西,则终止()

被调用。

Exceptions are always specified.
If foo() did not have an explicit exception specification then it
could 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.


9月15日,2:54 * am,raj s< yesr ... @ gmail.comwrote:
On Sep 15, 2:54*am, raj s <yesr...@gmail.comwrote:

什么是使用函数声明的优点和缺点

指定例外。

像foo()throws(char&)
What is the advantage and disadvantage of using function declaration
with exceptions specified.
like foo()throws(char&)



和一个小的挑剔它是无效的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; in
general use of exception specifications with the exception of nothrow
[void foo() throw() { }] is discouraged because it doesn''t enforce at
compile time (cf. Java).


这篇关于声明异常的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆