English 中文(简体)
为什么使用例外而不是......
原标题:Why use an exception instead of if...else
  • 时间:2011-11-17 01:32:40
  •  标签:
  • exception

For example, in the case of "The array index out of bound" exception, why don t we check the array length in advance:

 if(array.length < countNum)
 {
 //logic
 }
 else
 {
 //replace using exception
}

我要问的是,为什么选择使用例外? 以及何时使用例外而不是“条形”

感谢。

问题回答

它取决于特定语言的可接受做法。

在 Java,公约总是尽可能检查条件,而不是使用例外控制流动。 但是,例如,在沙尔,不仅以这种方式使用例外是可以接受的,而且是一种优先的做法。

它们被用来告知法典,称你的法典存在特殊条件。 如果根据逻辑,在例外情况下,如在你法典中达到条件,你不能在当地处理,或者支持你守则的召集人选择如何处理错误条件,则例外比正常的形成更加昂贵。

通常,如果你发现自己在职能或方法上 exceptions弃和追捕例外情形,你可能找到更有效的办法。

There are many answers to that question. As a single example, from Java, when you are using multiple threads, sometimes you need to interrupt a thread, and the thread will see this when an InterruptedException is thrown.

另一些时候,你将使用某种带有某些例外的APIC。 你们胜过,能够避免。 举例来说,如果APICA投影器投射出一个IOException,那么你就可以追捕它,或者让它迷惑不解。

这里的一个例子是,实际上最好使用例外而不是有条件。

Say you had a list of 10,000 strings. Now, you only want those items which are integers. Now, you know that a very small number of them won t be integers (in string form). So should you check to see if every string is an integer before trying to convert them? Or should you just try to convert them and throw and catch an exception if you get one that isn t an integer? The second way is more efficient, but if they were mostly non-integers then it would be more efficient to use an if-statement.

然而,如果你能够以有条件的方式取代例外,那么大多数时间都不应使用例外。

正如有人已经说过的那样,方案拟定语言的例外是例外情况,而不是确定方案的合理流动。 例如,就你的问题的某一法典而言,你必须看到,披露方法或功能的意图是什么。 正在核对<条码>、条码、条码/代码>部分业务逻辑。 如果是的话,则对<代码>if/else加以更正。 仍有路要走。 如果这一条件不属于商业逻辑的一部分,而封闭式方法的意图是其他的,那么就为这一目的写成法典,而不是采用<条码>。 例如,你制定学校申请,在你的申请中,你有一套方法<代码>。 GetClassTopperdes,负责商业逻辑部分,该部分要求将学生的最高分数退还给某个班级。 方法/功能定义类似:

int GetClassTopperGrades(string classID)

在这种情况下,根据申请的商业逻辑,这种方法的意图是将职等退还给有效职等,这总是是一种积极的分类。 现在,如果有人叫你的方法,穿过停车场或<代码>null,那么应该做些什么? 如果放弃一种例外情况,例如ArgumentException或ArgumentNullException,因为这是一个特殊情况下的 Unional案件。 这种方法假定,一定会通过有效的班级识别,而NUL或空洞是NOT的有效班级识别(偏离商业逻辑)。

除此之外,在某些条件下,没有事先知道特定代码的结果,也没有确定防止出现特殊情况的方法。 例如,询问一些遥远的数据库,如果网络倒塌,你除了放弃一个例外之外别无选择。 在向边远数据库发布每套电车之前,你是否检查网络连接?

Using exceptions is universal method to notify other part of code that something wrong happened in a loosely coupled way. Let imagine that if you would like to handle some exceptional condition by using if.. and else.. you need to insert into different part of your code some arbitrary variables and other assertions which probably would easily led to have spaghetti code soon after.

Let next imagine that you are using any external library/package and it s author decided to put in his/her code other arbitrary way to handle wrong states - it would force you to adjust to its way of dealing with it - for example you would need to check if particular methods returns true or false or some other provided variables has proper values. Using exceptions makes handling errors much more easy - you just assume that if something goes wrong - the other code will throw exception, so you just wrap the code in try block.





相关问题
handling exceptions IN Action Filters

Is there a better way to handle exceptions that occur inside an Action Filter itself in ASP .NET MVC? There re 2 ways I can think of at the moment. Using a try catch and setting the HTTP Status ...

既可捕获,又可举出例外。

我有一种办法,可以进入亚洲开发银行,因此,我国的亚行在多瑙河航道中的所有 st子都位于一个试捕区。 它正在追捕Kexception

Cross compiler exception handling - Can it be done safely?

I am doing some maintenance on a C++ windows dll library that is required to work with different VC++ compilers (as I don’t want to address different mangling schemes). I have already eliminated any ...

File Handling Issue

I am developing a tool in c#, at one instance I start writing into a xml file continuously using my tool,when i suddenly restart my machine the particular xml file gets corrupted, what is the reason ...

Watch a memory location/install data breakpoint from code?

We have a memory overwrite problem. At some point, during the course of our program, a memory location is being overwritten and causing our program to crash. the problem happens only in release mode. ...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签