About Question enthuware.ocpjp.v7.2.1204 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
RobynBackhouse
Posts: 23
Joined: Sun Apr 14, 2013 10:37 am
Contact:

About Question enthuware.ocpjp.v7.2.1204 :

Post by RobynBackhouse »

Hi there,
Quick question about this as I'm confused (again!)..
I have been playing around with it, and in your answer it says this:

Code: Select all

catch (FileNotFoundException | IndexOutOfBoundsException e) {             
    e = new IOException();
}
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.
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.
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();
}
Error:
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. :)

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1204 :

Post by admin »

You are right. Rules of assignment cannot change. For example, you can't assign a Car to a Plant! No matter what. This is irrespective of whether it is a multi catch or single catch. The explanation is trying to make that point but using a wrong example. It should be like this:

Code: Select all

catch (IOException | IndexOutOfBoundsException e) {             
    e = new FileNotFoundException(); //This would be valid if you have catch(IOException e)
    e = new IOException(); //This would be valid if you have catch(IOException e)
    e = new Exception(); //This would be valid if you have catch(Exception e) but not catch(IOException ioe)
}
Fixed.

thank you for your feedback!
If you like our products and services, please help us by posting your review here.

RobynBackhouse
Posts: 23
Joined: Sun Apr 14, 2013 10:37 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1204 :

Post by RobynBackhouse »

Oh I seeeee...
Yeah that makes sense now.
Many thanks! :D

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 22 guests