Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign values to catch parameter

Tags:

java

In the docs for multiple try-catch there is a statement.

If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block

But I don't get it. What do they mean by cannot assign any values? I can all every method of exception object as far as I remember. What assignment they are talking about?

like image 545
lapots Avatar asked Aug 17 '26 21:08

lapots


1 Answers

If you use a multi-catch then you cannot change the Exception reference;

try {
} catch (IOException|SQLException ex) {
  ex = null; // <-- NOT LEGAL, the ex is final.
}
like image 155
Elliott Frisch Avatar answered Aug 20 '26 11:08

Elliott Frisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!