Quick question about this as I'm confused (again!)..
I have been playing around with it, and in your answer it says this:
I'm not able to reassign the value of e to a new exception even in a catch clause with a single exception, unless it is to a new exception of the same type.The exception parameter in a multi-catch clause is implicitly final. Thus, it cannot be reassigned. Had there been only one exception in the catch clause, it would have been valid.Code: Select all
catch (FileNotFoundException | IndexOutOfBoundsException e) { e = new IOException(); }
e.g. This works fine, assigning e to a new FileNotFoundException:
Code: Select all
try { throw new FileNotFoundException("hello"); }
catch (FileNotFoundException e){
e = new FileNotFoundException("goodbye");
e.printStackTrace();
}
But this will not run, throwing an error when trying to assign it to a different type:
Code: Select all
try { throw new FileNotFoundException("hello"); }
catch (FileNotFoundException e){
e = new IOException("goodbye");
e.printStackTrace();
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from IOException to FileNotFoundException
Do you have any examples of how this works when assigning e to a different type of exception please? Or explain a little more how it works?
Many thanks.