How many compilation issues are in the following code?
1: public class Main { 2: class RainException extends Exception {} 3: // w w w . j a v a2s . c o m 4: public static void main(String[] args) { 5: try(Scanner s = new Scanner("rain"); String line = "";) { 6: if (s.nextLine().equals("rain")) 7: throw new RainException(); 8: } finally { 9: s.close(); 10: } 11: } 12: }
D.
Line 5 is incorrect because String does not implement AutoCloseable.
Not all objects can be declared in a try-with-resources try clause.
Line 7 is incorrect because RainException
is a checked exception and is not declared or handled.
Line 9 is incorrect because s is declared in the try clause and is therefore out of scope for the finally block.