Page 1 of 1

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

Posted: Sat Dec 03, 2011 6:28 pm
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?

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

Posted: Sun Dec 04, 2011 4:41 pm
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.