04-arraylist - 1 CSE 143 Lecture 4 Exceptions and ArrayList...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 CSE 143 Lecture 4 Exceptions and ArrayList slides created by Marty Stepp http://www.cs.washington.edu/143/ 2 Preconditions precondition : Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header: // Returns the element at the given index. // Precondition: 0 <= index < size public int get(int index) { return elementData[index]; } Stating a precondition doesn't really "solve" the problem Clients dont always follow directions. Precondition is non-binding What if we want to actually enforce the precondition? 2 3 Bad precondition test What is wrong with the following way to handle violations? // Returns the element at the given index. // Precondition: 0 <= index < size public int get(int index) { if (index < 0 || index >= size) { System.out.println("Bad index! " + index); return -1; } return elementData[index]; } -1 could be a legal value println is not a very strong deterrent to the client (esp. GUI) 4 Throwing exceptions (4.4) throw new ExceptionType (); throw new ExceptionType (" message "); Causes the program to immediately crash with an exception. Common exception types: ArithmeticException, ArrayIndexOutOfBoundsException, FileNotFoundException, IllegalArgumentException, IllegalStateException, IndexOutOfBoundsException, IOException, NoSuchElementException, NullPointerException, RuntimeException, UnsupportedOperationException Why would anyone ever want the program to crash? 2 3 Bad precondition test What is wrong with the following way to handle violations? // Returns the element at the given index. // Precondition: 0 <= index < size public int get(int index) { if (index < 0 || index >= size) { System.out.println("Bad index! " + index); return -1; } return elementData[index]; } -1 could be a legal value println is not a very strong deterrent to the client (esp. GUI) 4 Throwing exceptions (4.4) throw new ExceptionType (); throw new ExceptionType (" message "); Causes the program to immediately crash with an exception. Common exception types: ArithmeticException, ArrayIndexOutOfBoundsException, FileNotFoundException, IllegalArgumentException, IllegalStateException, IndexOutOfBoundsException, IOException, NoSuchElementException, NullPointerException, RuntimeException, UnsupportedOperationException Why would anyone ever want the program to crash? 3 5 Exception example public int get(int index) { if (index < 0 || index >= size) { String msg = "Out of list bounds: " + index; throw new IndexOutOfBoundsException(msg); } return elementData[index]; } Exercise: Modify the rest of ArrayIntList to state preconditions and throw exceptions as appropriate....
View Full Document

Page1 / 15

04-arraylist - 1 CSE 143 Lecture 4 Exceptions and ArrayList...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online