[HD Pg 282, Sec. 10.3.2 - throwing-exceptions-from-static-initializers]
Posted: Wed Jul 03, 2024 5:08 pm
hello
plz enlighten me
here you says: "Therefore, if a static initializer ends up throwing an exception, there is no way for the
application to handle that exception and to recover from it. For this reason, a static initializer is
not allowed to throw any checked exception. If the compiler sees a possibility of a checked exception
getting thrown out of a static initializer, it will generate an error"
But i still can do it with this:
plz enlighten me
here you says: "Therefore, if a static initializer ends up throwing an exception, there is no way for the
application to handle that exception and to recover from it. For this reason, a static initializer is
not allowed to throw any checked exception. If the compiler sees a possibility of a checked exception
getting thrown out of a static initializer, it will generate an error"
But i still can do it with this:
Code: Select all
public class Boo {
static int i = 5;
static{
try{
if(i == 5) throw new IOException();
}
catch(IOException e){
System.out.println("handle static checked exception");
}
}
public static void main(String[] args) {
Boo b = new Boo();
}
}