Page 1 of 1

About Question enthuware.ocpjp.ii.v11.2.1353 :

Posted: Wed Sep 16, 2020 6:54 am
by kabanvau
Hi,
This code will thow an Exception at runtime:

Code: Select all

System.out.println(rs.getString(0));
Columns are numbered from 1.

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Posted: Wed Sep 16, 2020 7:29 am
by admin
Did you read the explanation? To throw an exception, the code has to compile first!

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Posted: Wed Sep 16, 2020 7:40 am
by kabanvau
Yes, it does not compile. But it is wrong to number columns from 0.

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Posted: Wed Sep 16, 2020 8:28 am
by admin
It is true that it is wrong to number columns from zero. But neither the question nor the explanation says that it is ok to number columns from 0. It just that in this case, the given code does not compile, so the question of throwing exception does not arise.

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Posted: Sat Jan 09, 2021 8:35 am
by teodorj
In the explanation of this option...
It will not compile.
Observe that the declaration of try-with-resources statement is wrong. The type of the resource must be specified in the try itself. i.e. it should be: try(Statement stmt = c.createStatement())
Meaning that the resource used in try with resources in this example "stmt" must be final or effectively final or declare like in the explanation.
Am I correct?

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Posted: Sun Jan 10, 2021 1:10 am
by admin
Yes, the try-with-resources resource must either be a variable declaration or an expression denoting a reference to a final or effectively final variable.
Enhanced the explanation to make it clearer.
thank you for your feedback!

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Posted: Sun Jan 01, 2023 10:41 am
by supafly
at the bottom of the question: (Assume that items not specified such as import statements and try/catch block are all valid.)

the second part is a bit confuesing since the try with resource stmt is invalid.

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Posted: Sat Dec 16, 2023 7:29 am
by hsnclk
Even if we declare the Statement properly in the try-resource, we have an unhandled Exception problem caused by DriverManager.getConnection.

Image

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Posted: Sat Dec 16, 2023 9:48 pm
by admin
The unhandled exception problem that you are getting is because you are assuming that the code fragment is in a method without any throws clause. The note at the bottom is now updated as:
(Assume that the method in which this code appears has appropriate throws clause.)
thank you for your feedback!

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Posted: Sun Dec 17, 2023 8:34 am
by hsnclk
My pleasure :) I thank you!! It was really eye-opening question.