05-exceptions-testing - CSE 143 Lecture 5 More ArrayIntList:

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

View Full Document Right Arrow Icon
CSE 143 Lecture 5 More ArrayIntList : Pre/postconditions; exceptions; testing and JUnit reading: 15.2 - 15.3 slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Problem: size vs. capacity • What happens if the client tries to access an element that is past the size but within the capacity (bounds) of the array? – Example: list.get(7); on a list of size 5 (capacity 10) – Answer: Currently the list allows this and returns 0. • Is this good or bad? What (if anything) should we do about it? 5 size 0 5 0 6 5 4 0 7 0 8 9 2 7 3 0 8 3 value 9 1 0 index
Background image of page 2
3 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 void remove (int index) { return elementData[index]; } – Stating a precondition doesn't "solve" the problem, but it at least documents our decision and warns the client what not to do. What should we do if the client violates the precondition?
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Throwing exceptions (4.5) throw new ExceptionType (); throw new ExceptionType (" message "); • Causes the program to immediately crash with an exception. • Common exception types: – ArithmeticException, ArrayIndexOutOfBoundsException, FileNotFoundException, IllegalArgumentException, IllegalStateException, IOException, NoSuchElementException, NullPointerException, RuntimeException, UnsupportedOperationException • Why would anyone ever want a program to crash?
Background image of page 4
5 Exception example public void get(int index) { if (index < 0 || index >= size) { throw new ArrayIndexOutOfBoundsException (index); } return elementData[index]; } – Exercise: Modify the rest of ArrayIntList to state preconditions and throw exceptions as appropriate.
Background image of page 5

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

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

This note was uploaded on 10/30/2011 for the course COMPUTER S 143 taught by Professor John during the Spring '11 term at Washington State Community College.

Page1 / 18

05-exceptions-testing - CSE 143 Lecture 5 More ArrayIntList:

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

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