The following code generates this compiler warning:
finallyBug.java:12: Warning: Exception java.lang.InterruptedException must be caught, or it must be declared in throws clause of this method.
t.join();
^
1 warning
This warning is wrong because the exception cannot be propagated: the
try{...}finally{return;} surrounding t.wait() effectively catches and discards
all exceptions.
public
class finallyBug extends java.applet.Applet implements Runnable{
boolean v;
public void run(){
v = true;
}
static boolean foo( finallyBug b ){
try{
Thread t;
t = new Thread( b );
t.start();
t.join();
} finally {
return b.v;
}
}
public finallyBug(){v = false;}
public void init(){
System.out.println("foo() "+(foo( this )?"succeeded":"failed") );
}
public static void main( String ignore[] ){
System.out.println("foo() "+(foo( new finallyBug() )?"succeeded":"failed") );
}
}
finallyBug.java:12: Warning: Exception java.lang.InterruptedException must be caught, or it must be declared in throws clause of this method.
t.join();
^
1 warning
This warning is wrong because the exception cannot be propagated: the
try{...}finally{return;} surrounding t.wait() effectively catches and discards
all exceptions.
public
class finallyBug extends java.applet.Applet implements Runnable{
boolean v;
public void run(){
v = true;
}
static boolean foo( finallyBug b ){
try{
Thread t;
t = new Thread( b );
t.start();
t.join();
} finally {
return b.v;
}
}
public finallyBug(){v = false;}
public void init(){
System.out.println("foo() "+(foo( this )?"succeeded":"failed") );
}
public static void main( String ignore[] ){
System.out.println("foo() "+(foo( new finallyBug() )?"succeeded":"failed") );
}
}
- relates to
-
JDK-1224272 Bad code generated for try..finally{return;}
- Closed