哪种方法更好地一次捕获多个异常?

huangapple go评论72阅读模式
英文:

which one is the better approach to catch multiple exceptions at once?

问题

有许多种方法可以在编程中捕获“异常”。在程序内部捕获“已知”异常而不仅仅是捕获Exception是明智的做法。

为了减少不必要的重复代码片段,我们可以一次捕获多个异常,并进行进一步的操作。我找到了两种简单的方法来实现这一点。

方法1:

catch (IOException ex){
            System.out.println("找到异常");
            throw ex;
        }
catch (FileNotFoundException ex){
            System.out.println("找到异常");
            throw ex;
        }

方法2:

catch (IOException|FileNotFoundException ex){
            System.out.println("找到异常");
            throw ex;
        }

在Java中,哪种方法更好地处理多个异常?是否有更好的方法来实现这一点?

英文:

There are a number of ways to catch "Exceptions" in programming. It's always advisable to catch "known" exceptions inside programs without simply catch Exception.

To reduce unnecessary repetitive code fragments we can catch multiple exceptions at once and do further steps. I could find simple 2 methods to do that.

method 1:

catch (IOException ex){
            System.out.println("Exception found");
            throw ex;
        }
catch (FileNotFoundException ex){
            System.out.println("Exception found");
            throw ex;
        }

method 2:

catch (IOException|FileNotFoundException ex){
            System.out.println("Exception found");
            throw ex;
        }

Which one is the better method to handle multiple exceptions at one in java? is there any better method to do that?

答案1

得分: 1

请注意,你必须在捕获FileNotFoundException之前捕获IOException,因为FileNotFoundException是一种IOException。否则,你将永远无法到达FileNotFoundException。因此在你的情况下,你只能捕获IOException,因为它包含了FileNotFoundException

catch (IOException ex){ // does contain FileNotFoundException
    System.out.println("Exception found");
    throw ex;
}

一般来说:如果在捕获块中有相同的代码,你应该使用多重捕获,因为否则你将会有重复的代码。我在这方面看不出任何问题(事实上,这就是为什么多重捕获被引入到Java中的原因)。

但当然,并不总是这种情况。如果你必须以不同的方式处理异常,你必须定义不同的捕获块。

英文:

Please note, that you have to catch FileNotFoundException before catching IOException because a FileNotFoundException is an IOException. Otherwise you will never reach the FileNotFoundException. So in your case you could only catch IOException because it contains FileNotFoundException.

catch (IOException ex){ // does contain FileNotFoundException
    System.out.println("Exception found");
    throw ex;
}

Generally speaking: If you have the same code in your catch block, you should use a multi catch because otherwise you'd have duplicated code. I see no problem in that (in fact, that's why multi-catch has been introducted to Java).

But that's not always the case of course. If you have to treat exceptions differently you must define different catch blocks.

答案2

得分: -1

另一种方法是,在捕获块内部使用 if 条件来进行选择。

catch (Exception ex){
    if(ex == IOException){
        System.out.println("发现异常");
        throw ex;
    }
    else if (ex == FileNotFoundException){
        System.out.println("发现异常");
        throw ex;
    }
}
英文:

There is another method. we can use if condition inside catch block for selection.

catch (Exception ex){
if(ex==IOException){
      System.out.println("Exception found");
      throw ex;}
else if (ex==FileNotFoundException){
      System.out.println("Exception found");
      throw ex;}
            }
}

huangapple
  • 本文由 发表于 2020年10月23日 13:29:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64494367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定