About Question com.enthuware.ets.scjp.v6.2.720 :

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

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.2.720 :

Post by ETS User »

I have run this program and see that the answer is correct. What stumped me is that there is no return statement and assumed that it will be a compiler error. Can you explain why absence of return statement is being ignored by the compiler?

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

Re: About Question com.enthuware.ets.scjp.v6.2.720 :

Post by admin »

A method is not required to return a value if it ends abruptly i.e. if it throws an exception. In this case, the compiler notices that the method always throws an exception so it doesn't care for the return statement.

Compilation error occurs when the compiler sees a way or a situation in which the method is not returning a value and is not throwing an exception either.

For example, if you have:

if(someCondition) throw new Exception("oops");

It will realize that if someCondition returns false, the method will not return any value and it is will flag an error.
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 240 guests