COMPUTER 2430 Object Oriented Programming and Data Structures I
Exceptions
Class Stack public class Stack { private Object[] items; private int top = 0; public Object pop() return items[-- top]; } ... What if the stack is empty (top == 0)? Crash!
Class Stack public class Stack { private Object[] items; private int top = 0; /** Must call isEmpty before calling pop! */ public Object pop() return items[-- top]; } ... Can this avoid all crashes? NO!
Java Class Exception To avoid as many crashes as possible Throws exception objects Try and Catch
Throwing Exceptions public class Stack { private Object[] items; private int top = 0; /** Should make sure it’s not empty before calling pop. An exception will be thrown if empty. */ public Object pop() if ( top == 0 ) throw new Exception(“Empty Stack!”); return items[-- top]; } ...
Throwing Exceptions public class Stack { private Object[] items; private int top = 0; /** Should make sure it’s not empty before calling pop. An exception will be thrown if empty. */ public Object pop() throws Exception if ( top == 0 ) throw new Exception(“Empty Stack!”); return items[-- top]; } ...
Catching Exceptions Stack myStack = new Stack(100); Object obj; ... try { obj = myStack.pop(); // public Object pop() throws Exception // statement NEXT } catch ( Exception e ) System.out.println( e.toString() ); Won’t crash! Method pop is aborted. Statement NEXT will not be executed.
Try-Catch-Finally Stack myStack = new Stack(100); Object obj; ... try { obj = myStack.pop(); } catch ( Exception e ) System.out.println( e.toString() ); finally // Statements will always be executed, // even no exception is caught. Quiz is coming? Program due!
Method and Try-Catch-Finally public void DoWork( Stack myStack ) { try obj = myStack.pop(); } catch ( Exception e ) System.out.println( e.toString() ); // public Object pop() throws Exception finally ... // Clean up DoWork() handles the Exception! No “throws” on the method header. Quiz is coming? Program due!
Method and Try-Catch-Finally public void MoreWork() { Stack myStack; ... DoWork( myStack ); // public void DoWork( Stack myStack ) } Don’t need Try-Catch for Stack The exception is handled inside DoWork(). Quiz is coming? Program due!
Method and Try-Catch-Finally public void DoWork( Stack myStack ) throws Exception { ... obj = myStack.pop(); // public Object pop() throws Exception } DoWork() has no try-catch. Must throw Exception! Quiz is coming? Program due!
Try-Catch and Throws public void MoreWork() { Stack myStack; ... DoWork( Stack myStack ); //public void DoWork( Stack myStack ) throws Exception } Must Try-Catch Or Throws Exception Quiz is coming? Program due!
Class Exception Exception ArrayIndexOutOfBoundsException NullPointerException Other Exceptions User Exceptions Sub-Exceptions
Multiple Exceptions public void MyMethod() throws NullPointerException, ArrayIndexOutOfBoundsException { ... if ( . . . ) throw new ArrayIndexOutOfBoundsException(“Out of bounds”); else if ( . . . ) throw new NullPointerException(“No Object”); else } Quiz is coming? Program due!
Try-Catch-Finally try { ... } catch (ArrayIndexOutOfBoundsException e ) System.out.println( e.toString() ); catch (NullPointerException e ) ... // More catch blocks can be here finally Quiz is coming? Program due!
Try-Catch-Finally // Other exceptions will not be caught try { ... } catch (ArrayIndexOutOfBoundsException e ) System.out.println( e.toString() ); catch (NullPointerException e ) finally Quiz is coming? Program due!
Try-Catch-Finally // Catch all exceptions! try { ... } catch (ArrayIndexOutOfBoundsException e ) catch (NullPointerException e ) catch (Exception e ) finally Quiz is coming? Program due!
User Defined Exceptions public class StackEmptyException extends Exception { . . . // could have data public StackEmptyException ( String s ) super(s); . . . } . . . // We do not do this in CS 2430. Quiz is coming? Program due!
Throw StackEmptyException public class Stack { private Object[] items; private int top = 0; public Object pop() throws StackEmptyException if ( top == 0 ) throw new StackEmptyException (“Empty Stack!”); return items[-- top]; } ...
Catch StackEmptyException public void DoWork( Stack myStack ) { try obj = myStack.pop(); } catch (StackEmptyException e ) // catch (Exception e ) System.out.println( e.toString() ); finally ... // Clean up Quiz is coming? Program due!
Throws StackEmptyException public void DoWork( Stack myStack ) throws StackEmptyException { ... obj = myStack.pop(); } Quiz is coming? Program due!
Exceptions If a method throws an exception The header must indicate that public Object pop() throws Exception If a method calls another method that throws an exception Two options: 1. Try and Catch, or 2. The header must indicate that public void DoWork(Stack myStack) throws Exception
Static Main() public class Prog3 { ... public static void main( String [] args ) Scanner sc; try // throws an Exception if file not there sc = new Scanner( new File("Prog3_1.in") ); } catch (Exception ex) sc = new Scanner( System.in ); Quiz is coming? Program due!
Parameter args of Main() public class Prog3 { ... public static void main( String [] args ) String inFile = args[0]; String outFile = args[1]; } // Command line java Prog3 p3.in p3.out // args.length: 2 // args[0] : “p3.in” // args[1] : “p3.out” Quiz is coming? Program due!
Exercise public class TryExceptions { public void f1() throws Exception System.out.println("In f1"); throw (new Exception("F1 Exception")); } ... Quiz is coming? Program due!
public class TryExceptions { public void f1() throws Exception System public class TryExceptions { public void f1() throws Exception System.out.println("In f1"); throw (new Exception("F1 Exception")); } public void f2() try System.out.println("In f2"); f1(); catch ( Exception e ) System.out.println("Caught in f2"); Quiz is coming? Program due!
public class TryExceptions { public static void main(String [] arrgs) TryExceptions tr = new TryExceptions(); int x = 1, y = 1; while ( x < 4 && y < 4) try if ( x < 2 ) tr.f1(); tr.f2(); if ( x < 3 ) tr.f1(); System.out.println("At the bottom"); } catch ( Exception e ) ++ x; System.out.println( "Caught in Main" ); ++ y; System.out.println( "X: " + x + " Y: " + y ); Quiz is coming? Program due!
Schedule Lab 8 (5 points) Lab 7 (5points) Prog4 (20 points) Quiz 4 Due 11 pm, October 31 Lab 7 (5points) Due 11 pm, November 2 Prog4 (20 points) Due 11 pm, November 9 Quiz 4 Monday, November 5 Test 2 Wednesday, November 14