尝试捕获的目的 [英] Purpose of try catch

查看:68
本文介绍了尝试捕获的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++或java中使用try catch和throws的目的是什么?

解决方案

我们使用try / catch来包装可能导致异常的操作,这样我们就可以了可以处理它们。一般规则是,如果要对其执行某些操作,则应该只捕获异常 - 例如记录异常,或者首先重试导致异常的操作。如果你无法处理异常,你应该让它在应用程序中冒出来。



现在,为了获得异常,必须抛出它 - 这就是throw关键字是为了;抛出一个可以在catch块中处理的异常。


我最喜欢Petes解决方案(解决方案3),但我发现没有一个解决方案真正回答了这个问题(什么是的目的...)。所以这是我自己的看法:



异常是将错误情况报告给调用堆栈中较高级别的几种可能方法之一。通常,您不应使用异常来指示正常使用中期望的条件,即使它们无法继续执行您的函数。 E. g。如果你试图打开一个特定的数据文件,但找不到它,你应该通过一切通知用户的问题,但这个问题并不能保证异常处理。 OTOH,如果您调用系统函数并且结果数据的格式或值范围与预期不符并且通常不可能,则应该通知关键字的更高级别问题:那就是你 throw()异常。



简而言之:

例外不是期望

或稍微熟悉一点:

期待意外! (或者我应该把它变成 ex cep 意想不到的;))



当然,这有点弄巧成拙:如果你不期望某个价值或条件,那么你将如何对其进行测试?答案是反过来:测试你期望的价值观和条件,并将其他一切视为例外!



正如其他答复所指出的那样,你的函数抛出异常,任何调用你的函数(直接或间接)都应该考虑 catch 。如果他们不这样做,异常将被传递到更高级别,直到它到达main()。只有在您知道如何处理异常时,才应以这种方式使用 try / catch 。否则你不应该打扰并把它留给'更高层'。





注意有很多人考虑例外一个有效替换错误代码的机制:他们认为你不需要为'找不到文件'之类的东西返回错误代码,如果你有异常可以(ab)用于同一目的。但是:异常不是免费的,我不认为如果应用程序因找不到文件这样的消息而崩溃,用户会觉得很有趣,因为QA中没有人想要测试那个场景而没有高级函数的程序员感觉有责任抓住它!



使用错误代码肯定存在类似的问题,但除非未处理的错误代码以某种方式导致讨厌问题,至少应用程序不会崩溃到桌面,刺激用户。



至少这是我对它的看法 - YMMV


< blockquote>参考



http://www.cplusplus。 com / doc / tutorial / exceptions / [ ^

What is the purpose of using try catch and throws in c++ or java?

We use try/catch to wrap operations that could cause exceptions so that we can deal with them. There's a general rule that you should only catch an exception if you are going to do something with it - such as log the exception, or retry the operation that caused the exception in the first place. If you can't handle the exception, you should let it bubble up through the application.

Now, in order to get an exception, something has to throw it - and that's what the throw keyword is for; to throw an exception that can be handled in a catch block.


I like Petes solution best (solution 3), but I find that none of the solutions actually answer the question ("what is the purpose of..."). So here's my own take:

Exceptions are one of several possible ways to report error conditions to the higher levels in a call stack. In general, you should not use exceptions to indicate conditions that are expected in normal use, even if they are such that you cannot continue execution of your function. E. g. if you try to open a specific data file, but cannot find it, you should by all means inform the user of the problem, but this problem does not warrant exception handling. OTOH, if you call a system function and the resulting data is in a format or in a range of values that doesn't match the expectations and shouldn't normally be possible, then it's time to inform the 'higher levels' of a critical problem: that is where you throw() an exception.

In short:
exception is not expectation
or the somewhat more familiar:
expect the unexpected! (or maybe I should turn this into except the unexpected ;) )

Of course, this is somewhat self-defeating: if you don't expect a certain value or condition, then how would you test against it? The answer is to do the opposite: test for the values and conditions that you expect, and treat everything else as an exception!

As pointed out in other responses, when your function throws an exception, any function calling yours (directly or indirectly) should consider catching it. If they don't, the exception will be passed on to even higher levels, until it reaches main(). You should use try/catch in this manner only if you know what to do about the exception. Else you shouldn't bother and leave it to the 'higher-ups'.


Note that there are many people who consider exceptions a mechanic that effectively replaces error codes: they'd argue that you don't need to return error codes for stuff like 'file not found', if you have exceptions that can be (ab)used to the same end. However: exceptions don't come for free, and I don't think the user would find it funny if the application crashed with a message like 'file not found', because noone in QA thought to test for that scenario and none of the programmers of the higher-up functions felt responsible for catching it!

There are certainly similar problems associated with using error codes, but unless the untreated error code somehow causes a nasty problem, at least the application will not crash to desktop, irritating the user.

At least that's my take on it - YMMV


Refer

http://www.cplusplus.com/doc/tutorial/exceptions/[^]


这篇关于尝试捕获的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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