File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes missing return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "missing return statement" Watch "missing return statement" New topic
Author

missing return statement

prashantK Singh
Greenhorn

Joined: Sep 11, 2006
Posts: 19
hi all
what is the problem with return in the code below
class Test {
String f(int i){
switch(i){
case 0:return "A";
case 1:return "b";
case 2:return "c";
default:
assert false;
}
}
public static void main(String[] s){
Test t = new Test();
for(int i=0;i<4;i++){
System.out.println(t.f(i));
}
}
}
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,


the method will not return a String when "switched" with other values than 0,1 or 2.
So perhaps
in the default case.

The assertion is not a problem (considering compilation and run of the program).



... and please use the code button for code, otherwise you'll lose the indentation when you post.


Yours,
Bu.


all events occur in real time
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
The assert can be enabled and disabled at the runtime. Therefore, compiler will make sure there is a return path (except void case)for each possible flows.

[ October 11, 2006: Message edited by: wise owen ]
[ October 11, 2006: Message edited by: wise owen ]
prashantK Singh
Greenhorn

Joined: Sep 11, 2006
Posts: 19
thanks for quick reply
my problem is not about assert
i thinks i am not able to get "return" statement with switch
class Test {
String f(int i){
switch(i){
case 0:return "A";
}
}
public static void main(String[] s){
Test t = new Test();
for(int i=0;i<2;i++){
System.out.println(t.f(i));
}
}
}
this too is showing missing return statement
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
The switch statement executes the case corresponding to the value of the expression. Normally the code in a case clause ends with a break statement, which exits the switch statement and continues with the statement following the switch. If there is no corresponding case value, the default clause is executed. If no case matched and there is no default clause, execution continues after the end of the switch statement. Therefore, you need a return statement after switch statement.
Sharmila Rishi
Greenhorn

Joined: Oct 11, 2006
Posts: 20
Hi Prashant,
The error occurs cos if there is a method with a non-void return type,and if the method doesnt have a return statement....then each case statement must have a return or a throw statement(throw new AssertionError()).

Since there is no return statement in your default case, you get an error.

You need to have throw AssertionError() statement and not assert false cos asserions are disabled at runtime, by default.

Correct me if I m wrong!

Sharmila.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Sharmila Rishi wrote:
You need to have throw AssertionError() statement and not assert false cos asserions are disabled at runtime, by default.


No, AssertionErrors are Errors, and Errors are no checked exceptions, so you don't need to declare or handle them.
And you really should NEVER handle them, that's opposite to there intention.

Yours,
Bu.
Sharmila Rishi
Greenhorn

Joined: Oct 11, 2006
Posts: 20
I compiled the above program with the following correction

default: throw new AssertionError();


and it compiled and threw an assertion error at runtime, which is the expected behaviour....while

default:assert false;


didn't even compile.
So I guess the answer is right.


And BTW we dont have to handle the error, cos its not a checked exception....and aeertion errors should never be handled ...just throw it!!

Correct me if I m wrong.



-Sharmila Rishi.
[ October 11, 2006: Message edited by: Sharmila Rishi ]
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Sharmila Rishi wrote:
I
compiled the above program with the following correction

default: throw new AssertionError();
(...snip...)
default:assert false;
didn't even compile.



It didn't compile because it does not return a String.
If you throw an Exception, no return type is needed, because the program flow will just stop there (or jump into a catch block if you provide one).
Returning something after a throw would be also a compile time error (unreacheable code).

But you should not throw an AssertionError by hand, that's not were they are made for.
If you are sure, that the default case will never never been reached, you could assert false; or perhaps


but as the compiler does not know your logic, you still have to return something (or null as depicted) to compile your code.


And: AssertionErrors should neither been handle nor thrown directly, what do you want to achieve by this???


Yours,
Bu.
[ October 11, 2006: Message edited by: Burkhard Hassel ]
Sharmila Rishi
Greenhorn

Joined: Oct 11, 2006
Posts: 20
Thanks for the explanation.


-Sharmila Rishi.
prashantK Singh
Greenhorn

Joined: Sep 11, 2006
Posts: 19
Thanks Burkhard for the explanation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: missing return statement
 
Similar Threads
toString() on Integer class
Switch and Assertion
wrapper classes
toString() behaviour?
Regarding Assertion