Can static code blocks throw exceptions? [duplicate]
问题 This question already has answers here : Why doesn't Java allow to throw a checked exception from static initialization block? (8 answers) Closed 5 years ago . In a hypothetical situation I have a class like this: import java.io.File; import java.util.Scanner; class X { static Scanner scanner; static { scanner = new Scanner(new File("X.txt")); } } When compiling, I get unreported exeption java.io.FileNotFoundException ; must be caught or declared to be thrown because public Scanner(File