if else子句不能做什么,而异常处理可以做什么? [英] what can't be done with if else clause, and can be done with exception handling?

查看:205
本文介绍了if else子句不能做什么,而异常处理可以做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if else子句不能做什么,而异常处理可以做什么?

what can't be done with if else clause, and can be done with exception handling ?

换句话说,我们实际上需要在哪里使用异常处理和

In other words where do we actually need to use exception handling and mere if else won't serve the purpose.

异常处理只是显示错误的一种荣耀方式吗?

Is exception handling just a glorified way of showing errors ?

推荐答案

在最早的C ++实现中,或多或少地将异常编译为等效的if / else构造。而且它们像糖蜜一样缓慢,以至于您仍然可以找到编程指南,以它们很慢为理由对异常提出建议。

In the earliest C++ implementations, exceptions were compiled into equivalent if/else constructs, more or less. And they were slow as molasses, to the point where you can still find programming guides that recommend against exceptions on the grounds that "they are slow".

所以事实并非如此。 可以或不能的问题。这是可读性和性能的问题。

So it is not a question of "can" or "cannot". It is a question of readability and performance.

通过错误检查乱码代码的每一行,使人们更难遵循非异常(即常见)的情况。而且,正如每个优秀的程序员所知道的那样,您的代码的主要读者是人类读者。

Littering every line of your code with error checks makes it harder for a human to follow the non-exceptional (i.e. common) case. And as every good programmer knows, the primary audience for your code is human readers.

关于性能,如果/其他方面比现代异常实现。除非实际引发异常,否则现代实现实际上会产生零开销。如果您的例外情况确实代表例外情况,那么这可能是明显的性能差异。

As for performance, if/else is slower than a modern exception implementation. Modern implementations incur literally zero overhead except when an exception is actually thrown. If your exceptions truly represent "exceptional" cases, this can be a significant performance difference.

这篇关于if else子句不能做什么,而异常处理可以做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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