try/catch块的性能问题? [英] Performance issue with try/catch block ?

查看:105
本文介绍了try/catch块的性能问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了一些有关try/catch块的文章.

总的来说,try-catch是一个有用的语句,也是编程中的一个很好的概念.

但是有人说在我们的代码中使用try/catch块存在一些性能问题.是真的吗

在iOS(尤其是在内存有限的应用程序,例如移动应用程序)应用程序中使用try catch有什么问题吗?

使用try/catch是一个好习惯吗?

请对此提供帮助/建议.

I read some articles about try/catch blocks.

Overall, try-catch is a useful statement and one of the good concepts in programming.

But someone saying that there are some performance issue with using the try/catch blocks in our code. Is it true?

Is there any issue using try catch in iOS(especially in limited memory apps like mobile applications) applications?

Using try/catch is a good practice?

Please help/Suggest me in this.

Thanks in Advance.

推荐答案

使用try/catch是一种好习惯吗?"不只是这个.

不使用try/catch绝对不是一个好习惯.唯一可以接受的选择是不关心错误和其他异常情况(是的,在某些情况下这是完全可以接受的).

现在,按定义,try/catch不再是性能问题.如果您需要异常处理,那是不可避免的,如果您使用任何其他方式的错误处理(我什至不提这个想法),都会使性能变得更差.

不过,我发现尝试/捕获通常会带来巨大的性能问题.是的,相互矛盾的段落在起作用.我在上一段中所说的假定该代码是由非白痴编写的.但是,在现实生活中,您了解. :-)

尝试/捕获应该很少使用.理想情况下-一次出现在每个线程的堆栈顶部(以防止终止),并且一次出现在UI的主事件周期中.每次发生异常情况时都应该使用抛掷(顾名思义,并非总是错误,某些异常情况;经典示例:核反应堆过热").我们不会在特殊情况下浪费CPU时间,对.尝试/捕获的另一种情况-主要处理情况;试试/最后应该更典型;关闭文件句柄,处理等.

实际上,结构异常处理可以节省很多性能.程序设计应该更具进攻性,而不应具有防御性.不需要大多数检查.

请参阅我过去的答案中的一些建议:
我如何制作滚动条到达底部时将停止的循环 [当我运行应用程序时,异常是捕获了如何处理? [ throw. .then ... rethrowing [ ^ ],
未处理的异常:访问冲突 [
"Using try/catch is a good practice?" Not just this.

Not using try/catch is impossibly bad practice. The only acceptable alternative is not caring about errors and other exceptional conditions (yes, this is quite acceptable in some cases).

Now, try/catch can not be an issue of performance by definition. If you need exception handling, there are unavoidable, if you use any other way of error handling (I hate even mentioning the idea), you make performance much worse.

Nevertheless, I see that try/catch pretty often present huge performance problem. Yes, contradictory paragraphs in action. What I say in the previous paragraph assumed that the code is written by non-idiots. In real life, however… well, you understand. :-)

Try/catch should be used quite rarely. Ideally — once on the top of the stack of each thread (to prevent termination) and also in the main event cycle of the UI. Throw should be uses every time exceptional situation happens (not always an error, some exceptional situations, as the name suggests; classical example: "nuclear reactor overheated"). We do not spare CPU time for really exceptional situations, right. Another case of try/catch — primary specialize processing of the situation; try/finally should be more typical; close file handles, dispose, etc.

Actually, structure exception handling saves a lot of performance. Programming should be more offensive and less defensive. Most checks are not needed.

See some of my recommendations in my past answers:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^],
Unhandled Exception : Access Violation[^].

Only last answer was about C++, but most recommendations are applicable to many platforms/languages.

—SA


这篇关于try/catch块的性能问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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