捕获异常或某些类型异常之间是否有任何性能差异? - Java

MBH*_*MBH 2 java try-catch

我想知道这两个块之间是否存在差异或性能问题:

    try{
        return Integer.parseInt(numAsString);
    }catch (Exception e){
        return onErrorInt;
    }
Run Code Online (Sandbox Code Playgroud)

    try{
        return Integer.parseInt(numAsString);
    }catch (NumberFormatException e){
        return onErrorInt;
    }
Run Code Online (Sandbox Code Playgroud)

有时甚至在尝试内部有许多例外,例如:

    try{
        // open file then try to close
        // try to parse integer
        // another kind of exception throwing funcitons
    }catch (Exception e){
        return onErrorInt;
    }
Run Code Online (Sandbox Code Playgroud)

    try{
        // open file then try to close
        // try to parse integer
        // another kind of exception throwing funcitons
    }catch (NumberFormatException e){
        return // something;
    } catch (IOException e){
        // return same thing in the exception above
    }
Run Code Online (Sandbox Code Playgroud)

.

我正在做的是系统将每天24小时运行,每天重启1次.

在很多地方,我不关心Exception的类型,我只需要让我的应用程序一直运行.所以主要是关于性能的问题.

Ger*_*cso 9

性能差异?几乎没有.唯一的代价是迭代ExceptionTable,这是一个非常低调的内存操作.你可以在这里阅读关于内部的简短摘要:

JVM规范异常处理

区分异常类型的主要原因是允许开发人员在需要时对不同类型的异常采取不同的操作.