Section 12.2 Check Point Questions6 questions
12.2.1
What is the advantage of using exception handling?
12.2.2
Which of the following statements will throw an exception?
System.out.println(1 / 0); System.out.println(1.0 / 0);
12.2.3
Point out the problem in the following code. Does the code throw any exceptions?
long value = Long.MAX_VALUE + 1; System.out.println(value);
12.2.4
What does the JVM do when an exception occurs? How do you catch an exception?
12.2.5
What is the output of the following code?
public class Test { public static void main(String[] args) { try { int value = 30; if (value < 40) throw new ArithmeticException("value is too small"); } catch (ArithmeticException ex) { System.out.println("value is too small"); } System.out.println("Continue after the catch block"); } }What would be the output if the line
int value = 30; were changed to int value = 50;
12.2.6
Show the output of the following code.
(a) public class Test { public static void main(String[] args) { for (int i = 0; i < 2; i++) { System.out.print(i + " "); try { System.out.println(1 / 0); } catch (ArithmeticException ex) { System.out.println("Exception occurred"); } } } } (b) public class Test { public static void main(String[] args) { try { for (int i = 0; i < 2; i++) { System.out.print(i + " "); System.out.println(1 / 0); } } catch (ArithmeticException ex) { System.out.println("Exception occurred"); } } }