D
Duncan Strang
Hi
I'd be interested to hear what developers think about the following
issue that has been doing the rounds here at work.
Say we have the following code
public int confused(){
String foo = "bar";
int val = -1;
try{
val = Integer.parseInt(foo);
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
System.out.println("This always executes");
return val;
}
}
The question is:
What do you think about inserting a return within a finally block ?
The language spec appears ambiguous, also different versions of the
compiler behave differently, for example.
Compiling the above with j2se 1.4.1_02-b06 gives no warnings.
Compiling with j2se 1.4.2-b28 gives the following warning
Test.java [38:1] warning: finally clause cannot complete normally
}
^
1 warning
Finished Test.
I've read various articles and specs that seem to indicate that the
jvm handles the stack correctly (in terms of return addresses for
example) but the compiler still raises the warning.
All opinions gratefully accepted
Cheers
Duncan L.Strang
I'd be interested to hear what developers think about the following
issue that has been doing the rounds here at work.
Say we have the following code
public int confused(){
String foo = "bar";
int val = -1;
try{
val = Integer.parseInt(foo);
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
System.out.println("This always executes");
return val;
}
}
The question is:
What do you think about inserting a return within a finally block ?
The language spec appears ambiguous, also different versions of the
compiler behave differently, for example.
Compiling the above with j2se 1.4.1_02-b06 gives no warnings.
Compiling with j2se 1.4.2-b28 gives the following warning
Test.java [38:1] warning: finally clause cannot complete normally
}
^
1 warning
Finished Test.
I've read various articles and specs that seem to indicate that the
jvm handles the stack correctly (in terms of return addresses for
example) but the compiler still raises the warning.
All opinions gratefully accepted
Cheers
Duncan L.Strang