我应该使用异常而不是错误代码吗? [英] Should I use exceptions instead of error codes?

查看:60
本文介绍了我应该使用异常而不是错误代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

(因为该程序是用C ++编写的,并且讨论了一些与C ++相关的
元素,因此comp.lang.c ++,以及一般程序

设计问题,因此comp.programming。)


我正在用C ++制作这个bignum包。我想知道如何处理错误。
处理错误。现在大多数操作例程返回错误

代码如果失败 - 但是他们在其中使用的一些例程,或者b
重载操作符,将在失败时抛出异常。例如,

使用的C ++标准库例程,例如复制

向量或数字向量片段。这些可能会失败。

然后使用一致的错误处理系统是好的

其中bignum ops总是抛出异常而不是返回错误

代码,而不是有一些失败抛出异常和其他

失败发布错误代码(例外情况将来自

标准的lib。函数)?什么是理想?无论如何计划一个

bignum包?

Hi.
(crossposted because the program is in C++ and some C++-related
elements are discussed, hence comp.lang.c++, plus general program
design questions are asked, hence comp.programming.)

I''m making this bignum package in C++. I''m wondering though on how to
handle the errors. Right now most operations routines return error
codes if they fail -- but some routines they use inside them, or
overloaded operators, will throw exceptions on failure. For example,
the C++ standard library routines that get used, for instance to copy
vectors or pieces of vectors of digits. These may throw on failure.
Would it be good to then use a consistent system of error handling
where bignum ops always throw exceptions instead of returning error
codes, instead of having some failures throw exceptions and other
failures release error codes (the exceptions would be coming from the
standard lib. functions for example)? What is the "ideal" plan for a
bignum package, anyway?

推荐答案

mike3写道:
mike3 wrote:

嗨。

(因为该程序是用C ++编写的,并且讨论了一些与C ++相关的

元素,因此comp.lang.c ++,以及一般程序

设计问题,因此comp.programming。)


我正在用C ++制作这个bignum包。我想知道如何处理错误。
处理错误。现在大多数操作例程返回错误

代码如果失败 - 但是他们在其中使用的一些例程,或者b
重载操作符,将在失败时抛出异常。例如,

使用的C ++标准库例程,例如复制

向量或数字向量片段。这些可能会失败。

然后使用一致的错误处理系统是好的

其中bignum ops总是抛出异常而不是返回错误

代码,而不是有一些失败抛出异常和其他

失败发布错误代码(例外情况将来自

标准的lib。函数)?什么是理想?计划一个

bignum套餐,无论如何?
Hi.
(crossposted because the program is in C++ and some C++-related
elements are discussed, hence comp.lang.c++, plus general program
design questions are asked, hence comp.programming.)

I''m making this bignum package in C++. I''m wondering though on how to
handle the errors. Right now most operations routines return error
codes if they fail -- but some routines they use inside them, or
overloaded operators, will throw exceptions on failure. For example,
the C++ standard library routines that get used, for instance to copy
vectors or pieces of vectors of digits. These may throw on failure.
Would it be good to then use a consistent system of error handling
where bignum ops always throw exceptions instead of returning error
codes, instead of having some failures throw exceptions and other
failures release error codes (the exceptions would be coming from the
standard lib. functions for example)? What is the "ideal" plan for a
bignum package, anyway?



我喜欢这篇关于异常处理的文章:
http://neil.fraser.name/writing/exception/


11月16日上午11:14 ,mike3< mike4 ... @ yahoo.comwrote:
On Nov 16, 11:14 am, mike3 <mike4...@yahoo.comwrote:

嗨。

(因为该程序是用C ++和一些C ++进行交叉传递的相关的

元素被讨论,因此comp.lang.c ++,加上一般程序

设计问题,因此comp.programming。)

我正在用C ++制作这个bignum包。我想知道如何处理错误。
处理错误。现在大多数操作例程返回错误

代码如果失败 - 但是他们在其中使用的一些例程,或者b
重载操作符,将在失败时抛出异常。例如,

使用的C ++标准库例程,例如复制

向量或数字向量片段。这些可能会失败。

然后使用一致的错误处理系统是好的

其中bignum ops总是抛出异常而不是返回错误

代码,而不是有一些失败抛出异常和其他

失败发布错误代码(例外情况将来自

标准的lib。函数)?什么是理想?计划一个

bignum套餐,无论如何?
Hi.
(crossposted because the program is in C++ and some C++-related
elements are discussed, hence comp.lang.c++, plus general program
design questions are asked, hence comp.programming.)

I''m making this bignum package in C++. I''m wondering though on how to
handle the errors. Right now most operations routines return error
codes if they fail -- but some routines they use inside them, or
overloaded operators, will throw exceptions on failure. For example,
the C++ standard library routines that get used, for instance to copy
vectors or pieces of vectors of digits. These may throw on failure.
Would it be good to then use a consistent system of error handling
where bignum ops always throw exceptions instead of returning error
codes, instead of having some failures throw exceptions and other
failures release error codes (the exceptions would be coming from the
standard lib. functions for example)? What is the "ideal" plan for a
bignum package, anyway?



就个人而言,我更喜欢错误代码的例外情况。错误代码可能

仍未检测到,导致传播错误。

异常不会被检测到:或者他们可以但你必须明确代码
那个。


如果软件包中一个未检测到的错误可能导致大的混乱,那么我将使用异常报告错误。

我更喜欢一个大爆炸的应用程序崩溃,因为一个未被捕获的

异常,而不是一个很好的行为并且未被发现的大混乱。


我也是认为应用程序在其功能时更容易阅读

使用return返回预期的返回值。声明,相反

将它保存在通过引用传递的某个参数中。


您还可以实现一些报告堆栈跟踪的宏
$ b异常旅行的$ b(链式异常,或 http://www.ddj.com / cpp / 191100567

没有链接例外情况)


我的3美分(或2欧元)

Personally I prefer exceptions to error codes. Error codes could
remain undetected, causing an error to propagate.
Exceptions cannot go undetected: or they can but you have to
explicitly code that.

If an undetected error in the package could cause a big mess, then I
would report the error using an exception.
I prefer an application crash with a big bang because of an uncaught
exception than a big mess that acts nicely and goes undetected.

I also think that an application is easier to read when its functions
return the intended return value with the "return" statement, instead
of saving it in some parameter passed by reference.

You could also implement some macros that report the stack trace
traveled by the exception (chained exceptions, or http://www.ddj.com/cpp/191100567
without chaining the exceptions)

My 3 USD cents (or 2 eurocents)


在2007-11-16 05:14:46 -0500,mike3< mi ****** @ yahoo.comsaid:
On 2007-11-16 05:14:46 -0500, mike3 <mi******@yahoo.comsaid:

嗨。

(因为该程序是用C ++编写的,并且讨论了一些与C ++相关的元素b $ b元素,因此comp.lang.c ++,以及一般程序

设计问题,因此comp.programming。)


我正在用C ++制作这个bignum包。我想知道如何处理错误。
处理错误。现在大多数操作例程返回错误

代码如果失败 - 但是他们在其中使用的一些例程,或者b
重载操作符,将在失败时抛出异常。例如,

使用的C ++标准库例程,例如复制

向量或数字向量片段。这些可能会失败。

然后使用一致的错误处理系统是好的

其中bignum ops总是抛出异常而不是返回错误

代码,而不是有一些失败抛出异常和其他

失败发布错误代码(例外情况将来自

标准的lib。函数)?什么是理想?计划一个

bignum套餐,无论如何?
Hi.
(crossposted because the program is in C++ and some C++-related
elements are discussed, hence comp.lang.c++, plus general program
design questions are asked, hence comp.programming.)

I''m making this bignum package in C++. I''m wondering though on how to
handle the errors. Right now most operations routines return error
codes if they fail -- but some routines they use inside them, or
overloaded operators, will throw exceptions on failure. For example,
the C++ standard library routines that get used, for instance to copy
vectors or pieces of vectors of digits. These may throw on failure.
Would it be good to then use a consistent system of error handling
where bignum ops always throw exceptions instead of returning error
codes, instead of having some failures throw exceptions and other
failures release error codes (the exceptions would be coming from the
standard lib. functions for example)? What is the "ideal" plan for a
bignum package, anyway?



您应该首先列出可能的错误。在我的

头的顶部,我只看到两个:内存耗尽,并试图将
除以零。第一个引发异常,你无法修复,所以不要试试。后者也应该抛出异常。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者

标准C ++库扩展:教程和参考资料

www.petebecker.com/tr1book

You should begin by listing the possible errors. Off the top of my
head, I only see two: running out of memory, and attempting to divide
by zero. The first throws an exception, and you can''t fix it, so don''t
try. The latter should probably also throw an exception.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


这篇关于我应该使用异常而不是错误代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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