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

时间:2015-05-05 09:56:12

标签: java performance try-catch

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

以下示例代码:

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
    }
}

如果使用了if else,那么哪一个是个好主意呢?

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

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

修改

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

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

谢谢!

1 个答案:

答案 0 :(得分:1)

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