As mentioned above, if your code might cause a checked exception to be thrown; i.e.,:
public static void main(String[] args) throws FileNotFoundException, EOFException
{ // an uncaught FileNotFoundException or EOFException may be thrown here }
Only uncaught checked exceptions need to be listed in a method's throws clause. Unchecked exceptions can be caught in a try block, but if not, they need not be listed in the method's throws clause. Show
TEST YOURSELF #1 Consider the following program (assume that comments are replaced with actual code that works as specified): public class TestExceptions { static void e() { // might cause any of the following unchecked exceptions to be thrown: // Ex1, Ex2, Ex3, Ex4 } static void d() { try { e(); } catch (Ex1 ex) { System.out.println("d caught Ex1"); } } static void c() { try { d(); } catch (Ex2 ex) { System.out.println("c caught Ex2"); // now cause exception Ex1 to be thrown } } static void b() { try { c(); } catch (Ex1 ex) { System.out.println("b caught Ex1"); } catch (Ex3 ex) { System.out.println("b caught Ex3"); } } static void a() { try { b(); } catch (Ex1 ex) { System.out.println("a caught Ex1"); } catch (Ex4 ex) { System.out.println("a caught Ex4"); // now cause exception Ex1 to be thrown } } public static void main(String[] args) { a(); } }Assume that this program is run four times. The first time, method e throws exception Ex1, the second time, it throws exception Ex2, etc. For each of the four runs, say what is printed; if an uncaught exception is thrown, say what happens. solution
How to Define and Throw Exceptions
TEST YOURSELF #2 Question 1: Assume that method f might throw exceptions Ex1, Ex2, or Ex3. Complete method g, outlined below, so that:
static void g() throws ... { try { f(); } catch ( ... ) { ... } ... } Question 2: Consider the following method. static void f(int k, int[] A, String S) { int j = 1 / k; int len = A.length + 1; char c; try { c = S.charAt(0); if (k == 10) j = A[3]; } catch (ArrayIndexOutOfBoundsException ex) { System.out.println("array error"); throw new InternalError(); } catch (ArithmeticException ex) { System.out.println("arithmetic error"); } catch (NullPointerException ex) { System.out.println("null ptr"); } finally { System.out.println("in finally clause"); } System.out.println("after try block"); }Part A.
A. f(0, X, "hi"); Part B.
solution
Summary
Solutions to Self-Study Questions
Test Yourself #1What is printed for each of the four runs? 1. d caught Ex1 2. c caught Ex2 b caught Ex1 3. b caught Ex3 4. a caught Ex4 execution stops due to uncaught exception Ex1 thrown in main
Test Yourself #2Question 1: static void g() throws Ex1, Ex3 { try { f(); } catch (Ex1 ex1) { System.out.println("Ex1 caught"); } catch (Ex2 ex2) { System.out.println("Ex2 caught"); throw new Ex1(); } } Question 2: Part A. A. f(0, X, "hi"); nothing printed throws ArithmeticException B. f(10, X, ""); prints "in finally clause" throws StringIndexOutOfBoundsException C. f(10, X, "bye"); prints "array error", "in finally clause" throws InternalError D. f(10, X, null); prints "null ptr", "in finally clause", "after try block" Part B. Function f doesn't need to have a throws clause that lists the uncaught exceptions that it might throw because only uncaught CHECKED exceptions need to be listed in a method's throws clause. The uncaught exceptions that f might throw are all UNCHECKED exceptions. When you write a method that throws a checked exception you must?If a method declares a checked exception (i.e., an exception other than Error or RuntimeException), you must invoke it in a try-catch block or declare to throw the exception in the calling method.
When an exception is thrown by a method that is executing?8) When an exception is thrown by a method that is executing under several layers of method calls, a stack trace indicates the method executing when an exception occurred and all of the methods that were called in order to execute that method.
Which type of statement is used to throw an exception?All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class.
When the code in a try block may throw more than one kind of exception?When the code in a try block may throw more than one type of exception, you need to write a catch clause for each type of exception that could potentially be thrown. All of the exceptions that you will handle are instances of classes that extend this class.
|