THINK FIRST·CODE LATER

← Java Programming
Chapter 16 · Week 13

Exception Handling

Answered 0/38 Correct 0
Sign in to save progress across devices
Q1

Which statement about checked versus unchecked exceptions is correct?

Q2

Which class is the root of the exception hierarchy?

Q3

Which of the following is an unchecked exception?

Q4

What is the output of the following program?

public class TestArithmetic {
    public static void main(String[] args) {
        try {
            int data = 100 / 0;
            System.out.print("Try ");
        } catch (ArithmeticException e) {
            System.out.print("Catch ");
        }
        System.out.print("End");
    }
}
Q5

What is printed when the following code runs?

public class FinallyNoException {
    public static void main(String[] args) {
        try {
            int a = 10 / 2;
            System.out.print(a);
        } catch (Exception e) {
            System.out.print("Catch");
        } finally {
            System.out.print("Finally");
        }
    }
}
Q6

Consider the following catch clauses. What is the result?

public class MultiCatchOrder {
    public static void main(String[] args) {
        try {
            int[] arr = new int[2];
            arr[5] = 10;
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.print("Specific");
        } catch (Exception e) {
            System.out.print("General");
        }
    }
}
Q7

What happens if the two catch clauses of the previous question are swapped?

Q8

What does the following nested try block output?

public class NestedTry {
    public static void main(String[] args) {
        try {
            try {
                String s = null;
                System.out.print(s.length());
            } catch (NullPointerException e) {
                System.out.print("Inner ");
            }
            System.out.print("Middle ");
        } catch (Exception e) {
            System.out.print("Outer ");
        }
        System.out.print("End");
    }
}
Q9

When does a finally block not run?

Q10

What is the output?

try {
    System.out.print("A");
    int[] a = new int[1];
    a[2] = 0;
    System.out.print("B");
} catch (ArrayIndexOutOfBoundsException e) {
    System.out.print("C");
}
System.out.print("D");
Q11

Which exception is thrown by Integer.parseInt("12x")?

Q12

Which exception is thrown by "Java".charAt(10)?

Q13

Which exception results from 5.0 / 0?

Q14

Which catch clause would handle every exception the program could throw?

Q15

Which multi-catch clause is legal?

Q16

The parameter of a multi-catch clause is implicitly:

Q17

What is the difference between throw and throws?

Q18

Which method heading correctly declares that the method may propagate a checked exception?

Q19

A method body contains throw new IOException("x"); but the heading has no throws clause and no try. What happens?

Q20

The same method throws new IllegalArgumentException("x") instead. What happens?

Q21

How should a custom checked exception be declared?

Q22

Which constructor allows getMessage() to return a useful text?

Q23

What is the output?

public class Backup {
    static int attempt() {
        try { return 1; }
        finally { System.out.print("F"); }
    }
    public static void main(String[] a) { System.out.print(attempt()); }
}
Q24

What is the output?

public class Fortress {
    public static void main(String[] args) {
        try {
            System.out.print("open ");
            throw new RuntimeException("breach");
        } catch (RuntimeException e) {
            System.out.print(e.getMessage() + " ");
        } finally {
            System.out.print("close");
        }
    }
}
Q25

An exception thrown in a method with no matching catch:

Q26

What is the output?

public class Sleep {
    static void nap(int n) {
        if (n < 0) throw new IllegalArgumentException("negative");
        System.out.print("napping " + n);
    }
    public static void main(String[] a) {
        try { nap(-1); }
        catch (IllegalArgumentException e) { System.out.print("caught: " + e.getMessage()); }
    }
}
Q27

Which is the best practice?

Q28

Which situation is better handled by an if test than by catching an exception?

Q29

What does try-with-resources do?

Q30

Which exception does a Scanner throw when nextInt() meets the text "abc"?

Q31

What is the output?

public class WhackAnException {
    public static void main(String[] args) {
        int[] counts = new int[3];
        String[] words = { "a", "bb", "ccc", "dddd" };
        int i = 0;
        try {
            for (; i < words.length; i++) counts[i] = words[i].length();
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.print("stopped at " + i);
        }
    }
}
Q32

A checked exception must be handled or declared. “Declared” means:

Q33

Which of the following should normally not be caught?

Q34 Short answer

Explain what happens, statement by statement, when an exception is thrown in the middle of a try block that has both a matching catch and a finally, and say where execution resumes afterwards.

Q35 Short answer

Explain why catch (Exception e) placed before catch (ArithmeticException e) is a compile-time error, while the reverse order is fine.

Q36 Programming

Write a checked exception class TimeFormatException and a method public static int[] parseTime(String s) throws TimeFormatException that accepts a string of the form hh:mm, returns the hours and minutes as a two-element array, and throws the exception (with a helpful message) when the format is wrong or the values are out of range. Write a main that tests it with "09:45", "9-45" and "25:00", catching and reporting each failure.

Q37 Programming

Write an unchecked exception MessageTooLongException and a class Messenger whose send(String text) throws it when the text exceeds 160 characters. Demonstrate in main that the caller is not forced to catch it, and then catch it anyway and print the message length.

Q38 Programming

Write a class TripComputer with a method double averageSpeed(double distance, double hours) that throws an IllegalArgumentException for a negative distance and an ArithmeticException for zero hours, and a main that calls it three times in a loop, each call inside its own try with a multi-catch clause, printing which problem (if any) occurred each time and always printing done from a finally block.