CSE 331: Software Design and Implementation, Autumn 2026
Your job is to deliver code that you know works.
Trace.java is on your program sheet$ java Trace ⋮ Exception in thread "main" java.lang.ArithmeticException: cannot divide by zero at Trace.g(Trace.java:27) at Trace.f(Trace.java:21) at Trace.main(Trace.java:15)
g at line 27: the throw itselff at line 21: where it had called gmain at line 15: where it had called fg(n);: where does control go next?g returns; out of f if g throwsTraceCatch.java's main is on your program sheet; f and g are Trace'sf: done print for the call f(2)? For f(0)?main: done print?throw looks for a handler frame by frame down the stacktry with a catch for the exception's class? Control goes to ittry18static void f(int n) {
19 System.out.println(" f: calling g(" + n + ")");
20 try {
21 g(n);
22 } finally {
23 System.out.println(" f: finally");
24 }
25 System.out.println(" f: done");
26}main keeps its catch; f's try has only a finally: what prints?finally runs on the way out, whichever way out it is57try {
58 step(stack, token);
59} catch (CalcStack.CalcException e) {
60 System.out.println("error: " + e.getMessage());
61}step handles nothing; this one catch takes every throw in the stack5public class TraceSwallow {
6 public static void main(String[] args) {
7 try {
8 System.out.println("main: calling f(2)");
9 f(2);
10 System.out.println("main: calling f(0)");
11 f(0);
12 } catch (Exception e) {
13 }
14 System.out.println("main: done");
15 }catch for Exception takes a bug's throw as much as a user's60/**
61 * @modifies this
62 * @effects pops the top two numbers (first and second) and pushes
63 * second / first, rounded toward zero
64 * @throws TooFewOperandsException if the stack has fewer than two
65 * numbers, and leaves the stack unchanged
66 * @throws DivideByZeroException if the top number is zero, and
67 * leaves the stack unchanged
68 */
69void divide();@throws says what its exit leaves: the stack unchangedassertThrows, then assertEquals on what is leftp6 0 /, sees cannot divide by zero, then types pDraftStack's divide (program sheet), what does it print?{{ P }}
try {
body
{{ Q }}
} catch (X e) {
{{ E }}
handler
{{ Q }}
}
{{ Q }}E must be true at every throw of an X that can reach this catchmain's catch: the stack is as it was before this commandcatch: from every throw under its try