Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 2430 Day 22. Announcements Prog4 test document and Rational Junit tests are due this Friday at 2 PM Quiz 3 this Friday Quiz 4 next Friday Exam 2: 4/3/13.

Similar presentations


Presentation on theme: "CS 2430 Day 22. Announcements Prog4 test document and Rational Junit tests are due this Friday at 2 PM Quiz 3 this Friday Quiz 4 next Friday Exam 2: 4/3/13."— Presentation transcript:

1 CS 2430 Day 22

2 Announcements Prog4 test document and Rational Junit tests are due this Friday at 2 PM Quiz 3 this Friday Quiz 4 next Friday Exam 2: 4/3/13 –Review for exam 2 in lab on 4/2/13

3 Agenda Exceptions

4 Motivation public static void main(String args[]) { Stack theStack = new Stack(); Object obj = theStack.pop(); // Uh oh, pop from empty Stack!?! } What to do here? Just let the program crash? ($5) The pop() method could return null ? ($10) Use Exception s! (>$50)

5 The Exception class In Java, Exception s are Object s Can be “thrown” ( throw, throws ) Must be “caught” ( try, catch, finally )

6 The Exception hierarchy Exception RuntimeExceptionIOException... ArrayOutOfBoundsException... Throwable Object

7 Handling Exception s

8 Does this look familiar? try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } Let’s take a closer look!

9 The try block try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } Contains code that may throw an Exception, e.g. run()

10 The “suspect” code try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } Remember the signature for run() ? public void run() throws IOException

11 A catch block try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } If an IOException is thrown, it is caught by this (matching) catch block.

12 Handle the IOException try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } If IOException is thrown by run(), this code is executed

13 The finally block try { new Prog4().run(); } catch (IOException ex) { System.out.println("Something bad happened: " + ex); ex.printStackTrace(); } finally { System.out.println("Always executed"); } The (optional) finally block is always executed last

14 Exception handling rules A try block can have one or more catch blocks If an Exception is thrown, only the first matching catch block is executed If there is a finally block, it must be last –And it is always executed! Do NOT try - catch RuntimeExceptions ( NullPointerException, etc.)

15 Be careful! try {... } catch (Exception ex) { System.out.println("Exception"); } catch (IOException ioex) { System.out.println("IOException"); }

16 Be careful! try {... } catch (Exception ex) { System.out.println("Exception"); } catch (IOException ioex) { System.out.println("IOException"); } Syntax error! Why?

17 Children first, parents last try {... } catch (IOException ioex) { System.out.println("IOException"); } catch (Exception ex) { System.out.println("Exception"); } “Children must be caught before parents”

18 Defining your own Exception s

19 Just make a new class public class StackEmptyException {... // could have data public StackEmptyException(String msg) {... } } Not quite an Exception yet

20 Our very own Exception ! public class StackEmptyException extends Exception {... public StackEmptyException(String msg) { super(msg);... } } Most of the time, this is all we need for our own Exception s

21 When to throw a StackEmptyException ?

22 Before public class Stack { private Object[] items; private int top;... public Object pop() { return items[--top]; } }

23 After public class Stack { private Object[] items; private int top;... public Object pop() throws StackEmptyException { if (top == 0) throw new StackEmptyException("Empty stack, ya'll!"); return items[--top]; }

24 Exception may be thrown public class Stack { private Object[] items; private int top;... public Object pop() throws StackEmptyException { if (top == 0) throw new StackEmptyException("Empty stack, ya'll!"); return items[--top]; } The throws keyword specifies that a StackEmptyException may be thrown (back to the calling method)

25 throw an Exception public class Stack { private Object[] items; private int top;... public Object pop() throws StackEmptyException { if (top == 0) throw new StackEmptyException("Empty stack, ya'll!"); return items[--top]; } This statement throw s a new StackEmptyException back to where the method was initially called

26 Client code public void doSomething(Stack inStack) {... Object obj = inStack.pop(); System.out.println("Here is the object: " + obj);... } This is (now) a syntax error!

27 Must try - catch public void doSomething(Stack inStack) {... try { Object obj = inStack.pop(); System.out.println("Here is the object: " + obj); } catch (StackEmptyException ex) { System.out.println("Here is the exception: " + ex); }... }

28 Could also do this public void doSomething(Stack inStack) throws StackEmptyException {... Object obj = inStack.pop(); System.out.println("Here is the object: " + obj);... } In general: If the calling method does not try - catch the Exception, it must advertise that it throws the Exception to the calling method.

29 Bad practice public static void main(String args[]) throws Exception {... } You probably shouldn’t do this. Why?

30 Do NOT throw or throws RuntimeException s ( NullPointerException, etc.)


Download ppt "CS 2430 Day 22. Announcements Prog4 test document and Rational Junit tests are due this Friday at 2 PM Quiz 3 this Friday Quiz 4 next Friday Exam 2: 4/3/13."

Similar presentations


Ads by Google