About Question enthuware.ocpjp.v17.2.3709 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
gurban.azimli
Posts: 3
Joined: Wed Oct 04, 2023 2:56 pm
Contact:

About Question enthuware.ocpjp.v17.2.3709 :

Post by gurban.azimli »

Hi, can you please clarify why this option is considered as correct?

Every case must return a value.


if the case statement throws an exception. How this can be considered as returning value?

Code: Select all

var a = SomeEnum.A;
        String s = switch (a){
            case A -> "ada";
            case C -> throw new RuntimeException("qweqe");
            case B -> "qwejqle";
        };
in this example I believe case C does not return value instead it throws an exception.

Please clarify...

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

Re: About Question enthuware.ocpjp.v17.2.3709 :

Post by admin »

The "must return a value" is applicable only when the expression evaluation is completed normally. In case of an exception, the expression does not complete normally.
It is like saying a method whose return type is String must return a String. But the following method will compile even though it does not return a String:
String m(){
if(true) throw new RuntimeException();
}

But your point is valid, the option should mention this possibility to be correct. Will update.

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

pavvel
Posts: 9
Joined: Fri Dec 15, 2023 4:00 pm
Contact:

Re: About Question enthuware.ocpjp.v17.2.3709 :

Post by pavvel »

Switch expression can return void. How about that?
This goes against the correct answer: Every case must return a value or throw an exception.

switch ("A"){
default -> System.out.println("");
}

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

Re: About Question enthuware.ocpjp.v17.2.3709 :

Post by admin »

No, a switch expression cannot return void. It is not an expression in that case, it is a switch statement. Please go through the detailed explanation provided at the bottom of the question. It explains the difference between a switch statement and a switch expression.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 232 guests