了解断言和例外 [英] Understanding Assert and Exceptions

查看:82
本文介绍了了解断言和例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,我无法想到为什么我应该让

程序的逻辑抛出异常。除了捕获例外

并打印呃,哦之外到屏幕。我也认为在大多数情况下

没有办法正确处理异常,因为什么可以使用
来解决整数溢出?重新分配价值观?重启程序?

问题仍然存在。我认为一个整数溢出是

也不例外,但是拙劣的编程。


现在我想打印一条消息有一个bug - 我很抱歉比操作

系统提供的一些奇怪的错误消息更好

。比assert更好,当我编译

发布版本时会消失,如果那个bug仍然是

那么会导致奇怪的错误消息。


我的结论:抛出异常仍然比断言更好,因为

你总是可以在屏幕上打印一条用户友好的消息。您甚至可以提供一个对话框或其他内容,并要求用户在文本字段中写下导致该错误的

步骤。


我看到一个名叫lilburne的人说使用总是断言,除非你有

别无选择,但即便如此,也要考虑将该例外设计为

缺陷。


不知何故,我同意。毕竟,如果你检查错误情况,那么

在某种意义上你预计会发生这种情况并且也不例外

无论如何。

现在,如果你碰巧遇到任何异常情况并且你希望
抛出一个异常,这更多是编程不佳的借口,我想b $ b想想。再一次,关于整数溢出可以做些什么呢?或者是错误的

static_cast?

知道出现问题似乎是唯一的好处点

无论如何我都是例外。但话说回来,我全力以赴,因为他们不会给最终用户借口.b $ b b b b b b b b b b b但无论如何都必须这样做。

你怎么看?

Sometimes, I can''t think of any good reason why I should have the
program''s logic thrown an exception. Except for catching the exception
and printing "Uh, oh" to the screen. I also think that in most cases
there''s simply no way to handle an exception properly, because what can
one do about an integer overflow? Reassign values? Restart the program?
The problem will still be existing. I think that an integer overflow is
not an exception, but sluttish programming.

Now I think printing a message "There''s a bug - I''m so sorry" is much
better than some bizarre error message provided by the operating
system. Kind of better than assert, which disappears when I compile the
release version and causes the bizarre error message if the bug''s still
there.

My conclusion: throwing an exception is still better than assert, for
you can always print a user friendly message to the screen. You could
even provide a dialog or something and ask the user to write down the
steps that caused the bug in a text field.

I saw some guy named "lilburne" say "Use always assert, unless you have
no other choice, but even then consider the exception to be a design
flaw to be eliminated."

Somehow, I agree. After all, if you check for an error condition, then
in some sense you expect it to happen and it is no exception
whatsoever.

Now, if you happen to have any exceptional situations and you deside to
throw an exception, this is more an excuse for poor programming, I
think. Again, what can one do about an integer overflow? Or a wrong
static_cast?

Knowing that something went wrong seems to be the only good point about
exceptions to me anyway. But then again, I am all for assert, as they
don''t throw an excuse to the end user. But this has to be done, anyhow.
What do you think?

推荐答案

更正:。 ..因为他们不为最终用户提供借口。

Correction: ... as they don''t throw an excuse AT the end user.


发布:

有时,我不能想到为什么我应该让

程序的逻辑抛出异常。
Sometimes, I can''t think of any good reason why I should have the
program''s logic thrown an exception.



实际上并不是一个经验法则 - 它的直觉比什么都重要。


There''s isn''t really a rule of thumb -- it''s intuition more than anything.


我的结论:抛出异常仍然比断言好,因为

你总是可以在屏幕上打印一条用户友好的消息。你可以

甚至提供一个对话框或其他东西,并要求用户写下导致文本字段中的错误的

步骤。
My conclusion: throwing an exception is still better than assert, for
you can always print a user friendly message to the screen. You could
even provide a dialog or something and ask the user to write down the
steps that caused the bug in a text field.



当然,除非你劫持断言并使其更加用户友好。


Unless, of course, you hijack "assert" and make it more user-friendly.


我看到一个名为lilburne的人说使用总是断言,除非你有

别无选择,但即便如此,也要考虑将该例外设计为

缺陷。


不知何故,我同意。毕竟,如果你检查错误情况,那么

在某种意义上你期望它发生,它也不例外

无论如何。
I saw some guy named "lilburne" say "Use always assert, unless you have
no other choice, but even then consider the exception to be a design
flaw to be eliminated."

Somehow, I agree. After all, if you check for an error condition, then
in some sense you expect it to happen and it is no exception
whatsoever.



那些制定一揽子经验法则的人往往是不合格的。


People who make blanket rules of thumb like that tend to be improficient.


你怎么看?
What do you think?



我使用断言表示发生了错误......错误的事件,

,例如一封字母为7。


我在特殊情况下抛出异常已经发生了,例如当

随机数正好是256时。


-


Frederick Gotham


I use "assert" to indicate that something WRONG... W-R-O-N-G has happened,
such as a letter being ''7''.

I throw an exceptio when something exceptional has happened, such as when a
random number is exactly 256.

--

Frederick Gotham




Frederick Gotham写道:


Frederick Gotham wrote:


我用的是断言"表示发生了错误......错误的事件,

,例如一封字母为7。


我在特殊情况下抛出异常已经发生了,例如当

随机数正好是256.
I use "assert" to indicate that something WRONG... W-R-O-N-G has happened,
such as a letter being ''7''.

I throw an exceptio when something exceptional has happened, such as when a
random number is exactly 256.



为什么会有例外?


弗雷德,向我们展示一些真实的代码,你会在那个

实例中抛出异常。


我使用异常相当多(例如,在编译器中,源代码

有语法错误),但我不知道任何人会以你建议的方式使用异常

,除了开玩笑。


祝你好运,

Tom

Why would that warrant an exception?

Fred, show us some real code where you would throw an exception in that
instance.

I use exceptions quite a bit (e.g., in a compiler when the source code
has a syntax error), but I don''t know anyone who would use an exception
the way you suggest, except in jest.

Best regards,

Tom


这篇关于了解断言和例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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