Java try / catch性能,是否建议将try子句中的内容保持在最低限度? [英] Java try/catch performance, is it recommended to keep what is inside the try clause to a minimum?

查看:207
本文介绍了Java try / catch性能,是否建议将try子句中的内容保持在最低限度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到你有这样的代码:

Considering you have code like this:

doSomething() // this method may throw a checked a exception
//do some assignements calculations
doAnotherThing() //this method may also throw the same type of checked exception
//more calls to methods and calculations, all throwing the same kind of exceptions.

现在我知道,构造异常时实际上会遇到性能损失,特别是展开堆栈。我还阅读了一些文章,指出在输入try / catch块时会有轻微的性能损失,但没有一篇文章似乎有任何结论。

Now I know, there is in fact a performance hit when constructing the exception, specifically unwinding the stack. And I have also read several articles pointing to a slight performance hit when entering try/catch blocks, but none of the articles seem to conclude anything.

我的问题是,是它建议将try catch中的行保持为最小值?,即只在try子句中包含可以实际抛出正在捕获的异常的行。 try子句中的代码运行速度较慢或是否会导致任何性能损失?。

My question is, is it recommended to keep the lines inside the try catch to bare minimum?, i.e. ONLY have inside the try clause the lines that can actually throw the exception you are catching. Does the code inside the try clause run slower or cause any performance hit?.

但更重要的是,考虑到这一点,它是最佳实践/更易读的解决方案:

But more important what it is the best practice/more readable solution considering doing this:

try {
    doSomething() // this method may throw a checked a exception
//do some assignements calculations
doAnotherThing() //this method may also throw the same type of checked exception
//more calls to methods and calculations, all throwing the same kind of exceptions.
}
catch (MyCheckedException e) {
   //handle it
}

或:

try {
    doSomething() // this method may throw a checked a exception
}
catch (MyCheckedException e) {
   //Store my exception in a Map (this is all running in a loop and I want it to   continue running, but I also want to know which loops didn't complete and why)
   continue;     
} 
 //do some assignements calculations
try {
    doAnotherThing() // this method may throw a checked a exception
}
catch (MyCheckedException e) {
    //Store my exception in a Map (this is all running in a loop and I want it to   continue running, but I also want to know which loops didn't complete and why)
   continue;
} 

这是考虑到你将处理所有这些检查的异常完全相同的方式当然。

This is considering that you will handle ALL this checked exceptions exactly the same way of course.

推荐答案

在这里的示例中,如果doSomething()和doAnotherThing()都抛出异常,那么真正的性能提升。输入试用块很快,直到它抛出异常。

In your example here, the real performance hit is if both doSomething() and doAnotherThing() both throw exceptions. Entering a try-block is fast, until it throws an exception.

这真的取决于你的情况。如果你在任何方式抛出MyCheckedException时都需要做同样的事情,我认为将它们放在同一个try块中更具可读性和更高性能,但如果你需要以不同的方式处理两种不同的情况,那么当然将它们分开是更有意义的。

It really comes down to what your situation is. If you need to do the same thing when MyCheckedException is thrown either way, I'd consider it both more readable and more performant to have them both in the same try block, but if you need to handle the two different situations differently then of course it makes more sense to separate them.

编辑:我看了你的评论的结尾,你假设处理方式相同,在这种情况下我会把它们都在同一个试块中。

I read the end of your comment, you're assuming handling both the same way, in which case I'd put them both in the same try-block.

这篇关于Java try / catch性能,是否建议将try子句中的内容保持在最低限度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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