1
17-214
Principles of Software Construction: Objects, Design, and - - PowerPoint PPT Presentation
Principles of Software Construction: Objects, Design, and Concurrency Exceptions and contracts in Java Josh Bloch Charlie Garrod 17-214 1 Administrivia Homework 1 due Today 11:59 p.m. Everyone must read and sign our collaboration
1
17-214
2
17-214
3
17-214
4
17-214
5
17-214
FileInputStream fis = new FileInputStream(fileName); if (fis == null) { switch (errno) { case _ENOFILE: System.err.println("File not found: " + …); return -1; default: System.err.println("Something else bad happened: " + …); return -1; } } DataInput dataInput = new DataInputStream(fis); if (dataInput == null) { System.err.println("Unknown internal error."); return -1; // errno > 0 set by new DataInputStream } int i = dataInput.readInt(); if (errno > 0) { System.err.println("Error reading binary data from file"); return -1; } // The Slide lacks space to close the file. Oh well. return i;
6
17-214
FileInputStream fis = new FileInputStream(fileName); if (fis == null) { switch (errno) { case _ENOFILE: System.err.println(“File not found: “ + …); return -1; default: System.err.println(“Something else bad happened: “ + …); return -1; } } DataInput dataInput = new DataInputStream(fis); if (dataInput == null) { System.err.println(“Unknown internal error.”); return -1; // errno > 0 set by new DataInputStream } int i = dataInput.readInt(); if (errno > 0) { System.err.println(“Error reading binary data from file”); return -1; } // The Slide lacks space to close the file. Oh well. return i;
7
17-214
FileInputStream fileInput = null; try { fileInput = new FileInputStream(fileName); DataInputStream dataInput = new DataInputStream(fileInput); return dataInput.readInt(); } catch (IOException e) { System.err.println("Could not read int from file: " + e); return DEFAULT_VALUE; }
8
17-214
9
17-214
public static void main(String[] args) { try { test(); } catch (ArrayIndexOutOfBoundsException e) { System.out.println"("Caught index out of bounds exception: " + e); } } public static void test() { try { System.out.println("Top"); int[] a = new int[10]; a[42] = 42; // Index is too high; throws exception System.out.println("Bottom"); } catch (NegativeArraySizeException e) { System.out.println("Caught negative array size exception: " + e); } }
10
17-214
11
17-214
12
17-214
… … … … Checked Exceptions
13
17-214
14
17-214
15
17-214
16
17-214
// Empty catch block IGNORES exception – Bad smell in code! try { ... } catch (SomeException e) { }
17
17-214
FileInputStream fileInput = null; try { fileInput = new FileInputStream(fileName); DataInputStream dataInput = new DataInputStream(fileInput); return dataInput.readInt(); } catch (IOException e) { System.err.println("Could not read int from file: " + e); return DEFAULT_VALUE; }
18
17-214
FileInputStream fileInput = null; try { fileInput = new FileInputStream(fileName); DataInputStream dataInput = new DataInputStream(fileInput); return dataInput.readInt(); } catch (IOException e) { System.err.println("Could not read int from file: " + e); return DEFAULT_VALUE; } finally { // Close file if it’s open if (fileInput != null) { try { fileInput.close(); } catch (IOException ignored) { // No recovery necessary (or possible) } } }
19
17-214
20
17-214
try (DataInputStream dataInput = new DataInputStream(new FileInputStream(fileName))) { return dataInput.readInt(); } catch (IOException e) { System.err.println("Could not read file: " + e); return DEFAULT_VALUE; }
21
17-214
static void copy(String src, String dest) throws IOException { InputStream in = new FileInputStream(src); try { OutputStream out = new FileOutputStream(dest); try { byte[] buf = new byte[8 * 1024]; int n; while ((n = in.read(buf)) >= 0)
} finally { if (out != null) out.close(); } } finally { if (in != null) in.close(); } } }
22
17-214
static void copy(String src, String dest) throws IOException { try (InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest)) { byte[] buf = new byte[8 * 1024]; int n; while ((n = in.read(buf)) >= 0)
} }
23
17-214
24
17-214
25
17-214
25
26
17-214
/*@ requires array != null; @ @ ensures \result == @ (\sum int j; 0 <= j && j < array.length; array[j]); @*/ int total(int array[]);
postcondition precondition
27
17-214
28
17-214
/** * Returns the element at the specified position of this list. * * <p>This method is <i>not</i> guaranteed to run in constant time. * In some implementations, it may run in time proportional to the * element position. * * @param index position of element to return; * must be non-negative and less than the size of this list. * @return the element at the specified position of this list * @throws IndexOutOfBoundsException if the index is out of range * ({@code index < 0 || index >= this.size()}) */ E get(int index);
postcondition precondition exceptional behavior
29
17-214
30
17-214
31
17-214
32
17-214
33
17-214
34
17-214
35
17-214
36
17-214
37
17-214
38
17-214
39
17-214
40
17-214
41
17-214
42
17-214
43
17-214
44
17-214
45
17-214
46
17-214
47
17-214
48
17-214
49
17-214
50
17-214
51
17-214
final class PhoneNumber { private final short areaCode; private final short prefix; private final short lineNumber; ... @Override public String toString() { return String.format("(%03d) %03d-%04d", areaCode, prefix, lineNumber); } } PhoneNumber jenny = ...; System.out.println(jenny); Prints: (707) 867-5309
52
17-214
53
17-214
54
17-214
public final class PhoneNumber { private final short areaCode; private final short prefix; private final short lineNumber; @Override public boolean equals(Object o) { if (!(o instanceof PhoneNumber)) // Does null check return false; PhoneNumber pn = (PhoneNumber) o; return pn.lineNumber == lineNumber && pn.prefix == prefix && pn.areaCode == areaCode; } ... }
55
17-214
Whenever it is invoked on the same object more than once during an execution
same integer, provided no information used in equals comparisons on the
– If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. – It is not required that if two objects are unequal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
56
17-214
57
17-214
public final class PhoneNumber { private final short areaCode; private final short prefix; private final short lineNumber; @Override public int hashCode() { int result = 17; // Nonzero is good result = 31 * result + areaCode; // Constant must be odd result = 31 * result + prefix; // " " " " result = 31 * result + lineNumber; // " " " " return result; } ... }
58
17-214
public final class PhoneNumber { private final short areaCode; private final short prefix; private final short lineNumber; @Override public int hashCode() { return Objects.hash(areaCode, prefix, lineNumber); } ... }
59
17-214
60
17-214