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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
I compiled the above program with the following correction
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???