为什么需要异常处理? [英] Why do we need exception handling?

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

问题描述

我可以检查输入,如果是用户的无效输入,我可以使用一个简单的if条件打印输入无效,请重新输入(如果输入无效)。 / p>

这种方法如果有潜在的失败,使用if条件验证它,然后在遇到失败时指定正确的行为...似乎足够如果我基本上可以用这种方法来覆盖任何类型的失败(除以零等),为什么我需要这个整个异常处理机制(异常类)

解决方案

假设你有 func1 调用 func2 与一些输入。



现在,假设 func2 由于某种原因失败。



您的建议是处理 func2 中的失败,然后返回到 func1



如何 func1 知道什么erro r(如果有)发生在 func2 中,以及如何从该点开始?



要想到的是一个错误代码, func2 将返回,通常,零值将表示OK,而其他(非零)值中的每一个将表示一个特定的错误发生。



这种机制的问题是它限制了添加/处理新错误代码的灵活性。



使用异常机制,您有一个通用的异常对象,可以扩展到任何特定类型的异常。在某种程度上,它类似于错误代码,但它可以包含更多信息(例如,错误消息字符串)。



您仍然可以争论当然,那么,那么 try / catch 为什么呢?为什么不简单地返回这个对象?



幸运的是,这个问题已经在这里很详细地回答了:





一般来说,异常错误代码有两个主要优点:两者都是正确编码的不同方面:


  1. 有一个例外,程序员必须处理它或将其向上,而使用错误代码,程序员可能会错误地忽略它。 >


  2. 使用异常机制可以写入你的代码很清洁,并且具有一切自动处理的错误代码,你必须实现一个乏味的开关/ case ,可能在每个功能调用堆栈。



I can check for the input and if it's an invalid input from the user, I can use a simple "if condition" which prints "input invalid, please re-enter" (in case there is an invalid input).

This approach of "if there is a potential for a failure, verify it using an if condition and then specify the right behavior when failure is encountered..." seems enough for me.

If I can basically cover any kind of failure (divide by zero, etc.) with this approach, why do I need this whole exception handling mechanism (exception class and objects, checked and unchecked, etc.)?

解决方案

Suppose you have func1 calling func2 with some input.

Now, suppose func2 fails for some reason.

Your suggestion is to handle the failure within func2, and then return to func1.

How will func1 "know" what error (if any) has occurred in func2 and how to proceed from that point?

The first solution that comes to mind is an error-code that func2 will return, where typically, a zero value will represent "OK", and each of the other (non-zero) values will represent a specific error that has occurred.

The problem with this mechanism is that it limits your flexibility in adding / handling new error-codes.

With the exception mechanism, you have a generic Exception object, which can be extended to any specific type of exception. In a way, it is similar to an error-code, but it can contain more information (for example, an error-message string).

You can still argue of course, "well, what's the try/catch for then? why not simply return this object?".

Fortunately, this question has already been answered here in great detail:

In C++ what are the benefits of using exceptions and try / catch instead of just returning an error code?

In general, there are two main advantages for exceptions over error-codes, both of which are different aspects of correct coding:

  1. With an exception, the programmer must either handle it or throw it "upwards", whereas with an error-code, the programmer can mistakenly ignore it.

  2. With the exception mechanism you can write your code much "cleaner" and have everything "automatically handled", wheres with error-codes you are obliged to implement a "tedious" switch/case, possibly in every function "up the call-stack".

这篇关于为什么需要异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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