class exception_handling { public static void main(String args[]) { try { int i, sum; sum = 10; for (i = -1; i < 3 ;++i) sum = (sum / i); } catch(ArithmeticException e) { System.out.print("0"); } System.out.print(sum); } }
a) 0
b) 05
c) Compilation Error
d) Runtime Error
c
Explanation: Value of variable sum is printed outside of try block, sum is declared only in try block, outside try block it is undefined.
Output:
$ javac exception_handling.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
sum cannot be resolved to a variable