CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1
Summary Collection Framework – List – Map – Set 1-2
Final Exam Written Component – UML – Aggregation and Composition – Collection Framework – Inheritance (interfaces, abstract and concert classes) – Exception and error handling Lab component – Multiclass application 1-3
Sources of Errors The programmer The End-User The Runtime Environment 1-4
Exception Handling 1-5 Sources Error Valid Operation? Logical Error Exception Caught? Handler Runtime Error Yes No
The Delegation Model Handle or delegate policy – Detects an invalid operation in a method – Throws an exception – Searches the method for an exception handler – If the handler found then control is given to the it – If no, the search is transferred to the caller of the method – If no method can handle the throw exception, a runtime error occurs and the program terminated 1-6
Basic try-catch Construction try{ code fragment } catch (SomeType e) { exception handler } 1-7
Handling Multiple Exception try{ code fragment } catch (Type-1 e) { } catch (Type-3 e) { } catch (Type-3 e) 1-8
Other construction Finally Nested try-catch 1-9
The Throwable Hierarchy Object Throwable Exception RuntimeException Unchecked Exception Checked Exception Error 1-10
Object Oriented Exception Handling The substitutability principle – When a parent is expected, a child is accepted Throwable methods – getMessage() – printStackTrace() Explicit exception creation and throw ArithmeticException ae = new ArithmeticException() throw ae; 1-11
Building Robust Apps Validation versus Exception – Defensive Programming – Exception-Based programming 1-12
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0, “A negative amount!”);
Exercise 11.2 Locate the error and specify if it is compile- time, runtime, or logic try { // some code } catch (java.io.IOException e) { // some code } catch (java.io.FileNotFoundException e) { // some code }
Exercise 11.3 Explain what is wrong with the following try { // some code } catch (Exception e) {}
Exercise 11.4 Critique the following and compare to the previous exercise public static void main (String[] args) throws Exception
A Multiclass Application The abstract foods company UML see on page
Classes AbstractFods – Inventory – Journal – Contact Item Fresh Trx Contact Supplier – Catalog Client 1-18
Exception Examples The program reads a string of two slash- delimited integers, extracts two integers, divides them, and outputs their quotient. Modify the program to handle input errors 1-19
Summary Exception throw vs throws Catch Finally Check vs Uncheck 1-20
I/O Stream Keyboard input – Standard input – Keyboard source – Program data sink – Channel stream InputStream 1-21
System class in out 1-22
InputStream InputStream keyboard = System.in; int input = keyboard.read(); 1-23
Input Stream BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in)); 1-24
File input Open Stream BufferedReader filer = new BufferedReader (new InputStreamReader(new FileInputStream (fileName))); Reading for (String line = filer.readLine(); line != null; line = filer.readline()) { //process } Close Stream filer.close(); 1-25
Screen Output Standard Output Output Stream Standard Error PrintStream 1-26
Serialization java.io.objectOutputStream FileOutputStream fos = new FileOutputStream (fileName); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(gc); oos.close(); 1-27
Deserialization java.io.ObjectInputStream FileInputStream fis = FileInputStream(filename); ObjectInputStream ois = new ObjectInputStream(fis); Object obj = ois.readObject(); ois.close (); 1-28
Date Calendar Date/Format SimpleDateFormat 1-29
Exercise 10.1 Determine the interface that should be used for each of the following collections – The names of all students registered in a course – The letter grades obtained in a course – The names of your contacts and the telephone number of each – The reserved words in Java
Exercise 12.2 Write a program that – Creates an Item instance named Tuna with price $2.45 – Purchases 200 units for a total of $250 – Sells 50 units – Sells 25 units for a total of $30 – Purchases 100 units for a total of $175 – Outputs unit cost price
Exercise 12.3 Write a program that – Creates a Fresh instance called “Pacific Salmon”, with any item number, a price of $20, and an expiry date three weeks from now – Creates a Fresh instance called “Atlantic Salmon”, with the same item number as above, a price of $25, and an expiry date 20 days from now – Outputs the return of the equals method invoked on one passing the other as argument – Outputs the return of the toString method for the two items