如果在Java中循环使用try catch子句会影响性能吗? [重复]

时间:2021-10-11 04:07:23

This question already has an answer here:

这个问题在这里已有答案:

I want to know if I use one or two try catch in a while loop, does this affect the performance or not?

我想知道我是否在while循环中使用了一个或两个try catch,这是否会影响性能?

Example code below:

示例代码如下:

try {
    // some code
    ....

    try {
        // some code
        ...
    } catch(Exception e) {
        if(e is not catchable) {
            //throw it as MyException(e);
        }
    }

    // some code
    ...

} catch(Exception e) {
    if(e is my Exception) {
        then do somthing
        ...
    } else { // is other Exception
        if(e is not catchable) {
            then stop the loop
        }
        // else catch it and continue
    }
}

What about if else if used instead, then which one will be the good idea?

如果使用其他如果使用,那么哪一个是好主意呢?

If someone knows pleas tell me does it affect performance or not, if yes then why.

如果有人知道请求告诉我它是否影响性能,如果是,那么为什么。

My question maybe asked by someone else before, but I also want to know about if-else if it is better then try-catch.

我之前可能会问过我的问题,但是我也想知道if-else是否比try-catch更好。

Edit:

I know if throws Exception then for catching it needs some time, but how about if the program goes whit no Exception jut like:

我知道如果抛出Exception然后捕获它需要一些时间,但如果程序如何变为没有例外情况如下:

// some code
        ....
// some code
        ....
// some code
        ....

Thanks!

1 个解决方案

#1


Try - catch block doesn't affect performance itself, but the code inside catch block wil be executed every time an exception is throwed, and this could affects permformances.

Try - catch块本身不会影响性能,但每次抛出异常时都会执行catch块中的代码,这可能会影响性能。

#1


Try - catch block doesn't affect performance itself, but the code inside catch block wil be executed every time an exception is throwed, and this could affects permformances.

Try - catch块本身不会影响性能,但每次抛出异常时都会执行catch块中的代码,这可能会影响性能。