简体   繁体   中英

About Methods And Try, Catch Handler in Java

So, Here In Java I Am Not Understanding What is Exception e? What does e stand for, in a catch statement?

  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.

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 (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.

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

The Exception Catching thing is a long debate in Java world:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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