Strange Compiler Error

Hello,

I'm struggling to explain a compiler error I've just received. I had some original code which looked like this:

// Begin old java file

public class Foo

{

public String f1(int x, int y) throws Exception

{

if (x == 4)

{

return "foo4";

}

else if (x == 5)

{

return "foo5";

}

else if (x == 6)

{

return "foo6";

}

else

{

throw new Exception("Uh Oh");

}

}

public static void main(String[] args) throws Exception

{

Foo foo1 = new Foo( );

System.out.println(foo1.f1(5, 8));

}

}

// end old java file

This code compiles and runs fine. I then made this modification to the code:

// begin new java file

public class Foo

{

public String f1(int x, int y) throws Exception

{

if (x == 4)

{

return "foo4";

}

else if (x == 5)

{

return "foo5";

}

else if (x == 6)

{

if (y == 7)

{

return "foo7";

}

else if (y == 8)

{

return "foo8";

}

}

else

{

throw new Exception("jason");

}

}

public static void main(String[] args) throws Exception

{

System.out.println("In main ...");

Foo foo1 = new Foo( );

System.out.println(foo1.f1(5, 8));

}

}

// End new java file

Now the compiler complains about a missing return statement in f1( ). This doesn't make sense to me, can anyone explain? Thanks in advance!

-exits

[1626 byte] By [ExitsFunnela] at [2007-10-2 20:21:00]
# 1

Problem is here. If the value of x is 6, and y is NOT 7 or 8, the method doesn't return anything and won't goto the else block that throws Exception. By adding a return statment inside x== 6 block will sove the issue.

public String f1(int x, int y) throws Exception {

if (x == 4) {

return "foo4";

} else if (x == 5) {

return "foo5";

} else if (x == 6) {

if (y == 7) {

return "foo7";

} else if (y == 8) {

return "foo8";

}

return null; //Added return statment here.

} else {

throw new Exception("jason");

}

}

suppusa at 2007-7-13 23:03:26 > top of Java-index,Developer Tools,Java Compiler...