Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1.

Similar presentations


Presentation on theme: "CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1."— Presentation transcript:

1 CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1

2 Summary Collection Framework – List – Map – Set 1-2

3 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

4 Sources of Errors The programmer The End-User The Runtime Environment 1-4

5 Exception Handling 1-5 Sources Error Valid Operation? Logical Error Exception Caught? Handler Runtime Error Yes No

6 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

7 Basic try-catch Construction try{ code fragment } catch (SomeType e) { exception handler } 1-7

8 Handling Multiple Exception try{ code fragment } catch (Type-1 e) { } catch (Type-3 e) { } catch (Type-3 e) 1-8

9 Other construction Finally Nested try-catch 1-9

10 The Throwable Hierarchy Object Throwable Exception RuntimeException Unchecked Exception Checked Exception Error 1-10

11 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

12 Building Robust Apps Validation versus Exception – Defensive Programming – Exception-Based programming 1-12

13 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!”);

14 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 }

15 Exercise 11.3 Explain what is wrong with the following try { // some code } catch (Exception e) {}

16 Exercise 11.4 Critique the following and compare to the previous exercise public static void main (String[] args) throws Exception

17 A Multiclass Application The abstract foods company UML see on page 448 1-17

18 Classes AbstractFods – Inventory – Journal – Contact Item Fresh Trx Contact Supplier – Catalog Client 1-18

19 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

20 Summary Exception throw vs throws Catch Finally Check vs Uncheck 1-20

21 I/O Stream Keyboard input – Standard input – Keyboard  source – Program  data sink – Channel  stream InputStream 1-21

22 System class in out 1-22

23 InputStream InputStream keyboard = System.in; int input = keyboard.read(); 1-23

24 Input Stream BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in)); 1-24

25 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

26 Screen Output Standard Output Output Stream Standard Error PrintStream 1-26

27 Serialization java.io.objectOutputStream FileOutputStream fos = new FileOutputStream (fileName); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(gc); oos.close(); 1-27

28 Deserialization java.io.ObjectInputStream FileInputStream fis = FileInputStream(filename); ObjectInputStream ois = new ObjectInputStream(fis); Object obj = ois.readObject(); ois.close (); 1-28

29 Date Calendar Date/Format SimpleDateFormat 1-29

30 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

31 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

32 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


Download ppt "CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1."

Similar presentations


Ads by Google