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

Slides:



Advertisements
Similar presentations
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Advertisements

Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Index Exception handling Exception In Java Exception Types
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to –Improve the reliability.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Exceptions Chapter 18 Programs: DriverException2 DriverException TestAgeInputException.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exceptions Handling.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Exceptions and Assertions Recitation – 03/13/2009 CS 180 Department of Computer Science, Purdue University.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Java Exception Handling ● Exception = an event that occurs during the execution of a program that disrupts the normal flow of instructions: – Examples:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Chapter 12: Exception Handling
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
COSC 1P03 Data Structures and Abstraction 4.1 Abstract Data Types The advantage of a bad memory is that one enjoys several times the same good things for.
Class Design: Handling Errors Reading: 2 nd Ed: Chapter 15 3 rd Ed: Chapter 11 Exercises 2 nd Ed: P15.5, P15.6 (Hint: look at documentation for Scanner.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Exceptions CSC 171 FALL 2004 LECTURE 24. READING Read Horstmann Chapter 14 This course covered Horstmann Chapters
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
CSE 1020:Aggregation Mark Shtern 1-1. Summary Development process Testing Debugging 1-2.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Files and Serialization. Files Used to transfer data to and from secondary storage.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
CSE 1020:Using API Mark Shtern 1-1. Summary Delegation – Static Method, Object Client view vs Implementer view API, UML Errors Software Engineering Utility.
Introduction to OO Program Design
I/O Basics.
CSE 1020:Programming by Delegation
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,
Exercise 12.2 Write a program that
Web Design & Development Lecture 7
CSE 1020:Inheritance Mark Shtern.
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Exception Handling Contents
Java Basics Exception Handling.
The command invocation protocol
Presentation transcript:

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