简体   繁体   English

关于方法和尝试,Java 中的 Catch Handler

[英]About Methods And Try, Catch Handler in Java

So, Here In Java I Am Not Understanding What is Exception e?所以,在 Java 中,我不明白什么是异常 e? What does e stand for, in a catch statement?在 catch 语句中 e 代表什么?

  Thank You For correcting my statement!

It is the recommended sintax in catch block, the Exception is the class of the exception and the "e" is the variable that will hold the excepcion object instance.它是 catch 块中推荐的 sintax,异常是异常的 class,“e”是保存异常 object 实例的变量。

In some cases you will see that a method has more than one catch block so that different exceptions can be managed, in those cases the "e" will vary:在某些情况下,您会看到一个方法有多个 catch 块,以便可以管理不同的异常,在这些情况下,“e”会有所不同:

catch (FileNotFoundException fnfe) {
 // code to manage file not found exception
}
catch (IOException ioe) {
  // code to manage the I/O exception
}
catch (Exception e) {
  // code to manage generic exception
}

Remember that the order of the catch blocks matters , it goes from top to bottom so if there is a FileNotFoundException the rest will not be catched, as it is more specific.请记住, catch 块的顺序很重要,它是从上到下的,因此如果存在 FileNotFoundException,则不会捕获 rest,因为它更具体。

More on this, it is considered a good practice to be as specific as possible when catching exceptions, you can see some more tips in this article:更多关于这一点,在捕获异常时尽可能具体被认为是一种好习惯,您可以在本文中看到更多提示:

https://dzone.com/articles/9-best-practices-to-handle-exceptions-in-java https://dzone.com/articles/9-best-practices-to-handle-exceptions-in-java

The Exception Catching thing is a long debate in Java world:-)异常捕获是 Java 世界中的一个长期争论:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM