Contents. I Introductory Examples. Topic 06 -Exception Handling

Similar documents
COE318 Lecture Notes Week 10 (Nov 7, 2011)

Exception-Handling Overview

BBM 102 Introduction to Programming II Spring Exceptions

Programming II (CS300)

BBM 102 Introduction to Programming II Spring 2017

CS 3 Introduction to Software Engineering. 3: Exceptions

Exceptions - Example. Exceptions - Example

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

Programming II (CS300)

6.Introducing Classes 9. Exceptions

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

Download link: Java Exception Handling

CSCI 261 Computer Science II

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Comp 249 Programming Methodology Chapter 9 Exception Handling

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Exception Handling. Chapter 9

Programming Language Concepts: Lecture 7

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Exception Handling Generics. Amit Gupta

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

ECE 122. Engineering Problem Solving with Java

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

What can go wrong in a Java program while running?

School of Informatics, University of Edinburgh

Introduction to Software Design

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Programming Language Concepts: Lecture 7

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Java Loose Ends. 11 December 2017 OSU CSE 1

Exceptions in Java

COMP1008 Exceptions. Runtime Error

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

Exceptions Handling Errors using Exceptions

Exceptions. CSC207 Winter 2017

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Administrivia. CPSC Winter 2008 Term 1. Department of Computer Science Undergraduate Events

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

CS193j, Stanford Handout #25. Exceptions

Data Structures. 02 Exception Handling

Exceptions and Libraries

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Object Oriented Programming Exception Handling

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

Internal Classes and Exceptions

Chapter 12 Exception Handling

Program Correctness and Efficiency. Chapter 2

Projeto de Software / Programação 3 Tratamento de Exceções. Baldoino Fonseca/Márcio Ribeiro

CMSC 202. Exceptions

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

CS159. Nathan Sprague

Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

Input-Output and Exception Handling

EXCEPTIONS. Java Programming

Chapter 13 Exception Handling

Object Oriented Programming

COMP-202. Exceptions. COMP Exceptions, 2011 Jörg Kienzle and others

CS Programming I: Exceptions

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

13: Error Handling with Exceptions. The basic philosophy of Java is that "badly formed code will not be run."

Chapter 14. Exception Handling and Event Handling ISBN

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

EXCEPTIONS. Prof. Chris Jermaine

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

CS 112 Programming 2. Lecture 08. Exception Handling & Text I/O (1) Chapter 12 Exception Handling and Text IO

Object Oriented Software Design

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Object Oriented Software Design

CS Programming I: Exceptions

17. Handling Runtime Problems

Lecture 19 Programming Exceptions CSE11 Fall 2013

Exception Handling. Chapter 8. Chapter 8 1

Fundamentals of Object Oriented Programming

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

Java Errors and Exceptions. Because Murphy s Law never fails

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED


ITI Introduction to Computing II

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

CS112 Lecture: Exceptions and Assertions

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

Assertions and Exceptions Lecture 11 Fall 2005

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

C16b: Exception Handling

Exceptions. Examples of code which shows the syntax and all that

CSE 143 Lecture 25. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides adapted from Marty Stepp

ITI Introduction to Computing II

Sri Vidya College of Engineering & Technology Question Bank

Transcription:

Contents Topic 06 - Handling I. Introductory Examples (Example 1-5) II. Handling Basic Idea III. Hierarchy IV. The Try-throw-catch Mechanism V. Define our own classes (Example 6) VI. Pitfall: Catch the more specific first (Example 7) VII. The throws clause, throws clause in derived class (Example 8) VIII. The Catch-or-Declare Rule, Checked s, Unchecked s IX. The Finally Block (Example 6') I Introductory Examples Files for testing: Requirement: Read one positive integer from a file Example 1 - Problems checked by Java Virtual Machine throws FileNotFound Scanner in = new Scanner(System.in); System.out.print("Input the file pathname: "); String fname = in.next(); [Line 13] [Line 14] in.close(); Rundown 1.1: Input the file pathname: c:\data001.txt Data is: 678 Rundown 1.2: Input the file pathname: c:\data02.txt in thread "main" java.io.filenotfound at java.io.fileinputstream.open(native Method) at java.io.fileinputstream.<init>(unknown Source) at java.util.scanner.<init>(unknown Source) at Main.main(Main.java:13) Rundown 1.3: Input the file pathname: c:\data002.txt in thread "main" java.util.inputmismatch at java.util.scanner.throwfor(unknown Source) at java.util.scanner.next(unknown Source) at java.util.scanner.nextint(unknown Source) at Main.main(Main.java:14) Problems are checked by Java Virtual Machine java.io.filenotfound and java.util.inputmismatch are JAVA classes, each means a type of. JVM outputs the message by calling: public void printstacktrace() [a method of the Java.lang.Throwable class] Last modified: 8-Nov-2018 1/12

Files for testing: Example 2 - We check and handle the problems ourselves Below shows how that can be done, using -catch blocks Program: Scanner in = new Scanner(System.in); System.out.print("Input the file pathname: "); String fname = in.next(); A Try block tells what to do when everything goes smoothly. A catch block handles a kind of caught problems (s) in.close(); catch (FileNotFound e) System.out.println("Cannot open the file. Please check or ask CS2312 helpers. ); catch (InputMismatch e) System.out.println("Cannot read the required number from the opened file. "+ " Please download from Helena's website again."); catch (InputMismatch e) Declare the type (Data type) and object variable (other names are ok, but e is often used) Testing: Rundown 2.1: Input the file pathname: c:\data001.txt Data is: 678 Rundown 2.2: Input the file pathname: c:\data02.txt Cannot open the file. Please check or ask CS2312 helpers. catch (FileNotFound e) System.out.println("Cannot open ); catch (InputMismatch e) System.out.println("Cannot read ); Rundown 2.3: Input the file pathname: c:\data002.txt Cannot read the required number from the opened file. Please download from Helena's website again. We write code to check and take action (here simply output the situation) Last modified: 8-Nov-2018 2/12

A common case: we handle more processing: 1) Put the actions in one method. The actions may have problems. 2) Place the -catch block in the caller. The caller will handle the problems. Example 3 public static void processfile(string fname) throws FileNotFound, InputMismatch The throws clause declare what s might occur. Scanner in = new Scanner(System.in); System.out.print("Input the file pathname: "); String fname = in.next(); processfile(fname); in.close(); catch (FileNotFound e) System.out.println("Cannot open the file. Please check or ask CS2312 helpers."); catch (InputMismatch e) System.out.println("Cannot read the required number from the opened file. Please download from Helena's website again."); Rundown: [Same as 2.1, 2.2, 2.3 in Example 2] We can write an Controlled Loops - Let the user get things right on a subsequent Example 4 public static void processfile(string fname) throws FileNotFound, InputMismatch Scanner in = new Scanner(System.in); boolean shouldend=false; while (!shouldend) System.out.print("Input the file pathname: "); String fname = in.next(); processfile(fname); shouldend=true; catch (FileNotFound e) System.out.println("Cannot open the file."); System.out.print("Try another file? Type your choice [y/n]: "); shouldend=(in.next().charat(0)=='n'); Rundown 4.1: Input the file pathname: c:\data002.txt Cannot read the required number from the opened file. Try another file? Type your choice [y/n]: y Input the file pathname: c:\data02.txt Cannot open the file. Try another file? Type your choice [y/n]: y Input the file pathname: c:\data001.txt Data is: 678 catch (InputMismatch e) Rundown 4.2: Input the file pathname: c:\data002.txt System.out.println("Cannot read the required number Cannot read from the the required opened number file."); from the opened file. System.out.print("Try another file? Type your Try choice another [y/n]: file? Type "); your choice [y/n]: y shouldend=(in.next().charat(0)=='n'); Input the file pathname: c:\data02.txt Cannot open the file. Last modified: in.close(); Try another file? Type your choice [y/n]: n 8-Nov-2018 3/12

Create class + throw an object of the kind - Suppose we expect a non-negative integer, however the file contains a ve integer. This is what we want: If the file content is ve, the output should tell: Rundown 5.1: Input the file pathname: c:\data003.txt Unexpected negative value from the file. Try another file? Type your choice [y/n]: n - This problem is not described by JAVA standard classes. Therefore we create and use our own class. Example 5 public class NegativeInteger extends public NegativeInteger() super("negative integer!"); public NegativeInteger(String message) super(message); It is customary to give both a default constructor and a constructor that contains a detailed message. processfile(): public static void processfile(string fname) throws FileNotFound, InputMismatch, NegativeInteger May throw FileNotFound if (x<0) throw new NegativeInteger(); May throw InputMismatch throw NegativeInteger Inside main(): System.out.print("Input the file pathname: "); String fname = in.next(); processfile(fname); shouldend=true; catch (FileNotFound e). catch (InputMismatch e). catch (NegativeInteger e) System.out.println("Unexpected negative value from the file."); System.out.print("Try another file? Type your choice [y/n]: "); shouldend=(in.next().charat(0)=='n'); Last modified: 8-Nov-2018 4/12

II Handling Basic Idea Possible errors of a running JAVA program 1) User input error (e.g. typing mistake, wrong format of URL) 2) Device errors (e.g. printer suddenly jammed, webpage temporarily unavailable) 3) Physical limitation (e.g. disk full, out of memory) 4) Code error (e.g. invalid array index) Error handling 1) [Traditional approach I ] Method returns -1, special end-of-file value, etc 2) [Traditional approach II] Method returns a null reference. 3) JAVA s handling: [Mission] Transfer control from the location where error occurred to an error handler that can deal with the situation throws an object that encapsulates the error information the code section exits immediately it does not return any value (even the method return type is not void) Search for an handler that can deal with this particular error. III Hierarchy in JAVA http://docs.oracle.com/j avase/7/docs/api/java/u til/inputmismatchexcep tion.html The Topmost Class: Throwable http://docs.oracle.com/javase/7 /docs/api/java/lang/throwable. html Throwable is the superclass of all errors and s in JAVA, - Thrown by JVM, e.g. FileNotFound InputMismatch Throwable Last modified: 8-Nov-2018 5/12 Error - Thrown in our code, e.g. NegativeInteger (we defined in Example 5) IO Runtime

The Hierarchy: [http://docs.oracle.com/javase/7/docs/api/java/lang/error.html] The hierarchy indicates conditions that a reasonable application might want to catch. (Ref. Examples 1-5) Error Throwable IO Runtime Example 5 [recalled] public static void processfile(string fname) throws if (x<0) throw new NegativeInteger(); throw NegativeInteger JVM may throw FileNotFound JVM may throw InputMismatch. Catch and handle processfile(fname);. catch (FileNotFound e) catch (InputMismatch e) catch (NegativeInteger e). The Error Hierarchy: [http://docs.oracle.com/javase/7/docs/api/java/lang/.html] The Error hierarchy describes internal errors and resource exhaustion situations which are abnormal. Indicates serious problems that a reasonable application should NOT to catch. (But if you want, you can!!) Error Throwable IO Runtime It should be aborted and the system (not our code) need to take over the control. St Note: Java programmers often call Errors as s as well. We speak in the same way in this topic. Examples: java.lang.outofmemoryerror (Previous lecture exercise: out of heap space) Object[] arr1 = new Object[10000000]; Object[] arr = arr1; for (int i=0;i<17;i++) arr[0]=new Object[10000000]; arr=(object[])arr[0]; Program aborted by JAVA runtime in thread "main" java.lang.outofmemoryerror: Java heap space at Main.main( Main.java:12) java.lang.stackoverflowerror (Ref. Lec06_Ex: recursion cannot stop) private static int factorial(int n) if (n==1) return 1; else return n*factorial(n+1); Program aborted by JAVA runtime in thread "main" java.lang.stackoverflowerror at MainStackError.factorial(MainStackError.java:5) at MainStackError.factorial(MainStackError.java:6) at MainStackError.factorial(MainStackError.java:6) at MainStackError.factorial(MainStackError.java:6) System.out.println(factorial(4));. Last modified: 8-Nov-2018 6/12

Catch an error and handle it, continue running. [For illustration purpose only; recall: a reasonable application should NOT to catch ] private static int factorial1(int n) if (n==1) return 1; else return n*factorial1(n+1); private static int factorial2(int n) if (n==1) return 1; else return n*factorial2(n-1); We catch the error and handle it, make the program continue to run However, practically it should be aborted and the system (not our code) need to take over the control. Scanner in = new Scanner(System.in); System.out.println(factorial1(4)); //failed catch (StackOverflowError e) System.out.printf("Error happens when factorial1 runs\n\n"); System.out.print("Print stack? Type your choice [y/n]: "); if (in.next().charat(0)=='y') e.printstacktrace(); System.out.print("\nTry another one? Type your choice [y/n]: "); if (in.next().charat(0)=='y') System.out.println(factorial2(4)); //OK: 24 System.out.println("\nFinished"); Error happens when factorial1 runs Print stack? Type your choice [y/n]: y java.lang.stackoverflowerror at MainStackErrorCaught.factorial1(MainStackErrorCaught.java:7) at MainStackErrorCaught.factorial1(MainStackErrorCaught.java:8) at MainStackErrorCaught.factorial1(MainStackErrorCaught.java:8) at MainStackErrorCaught.factorial1(MainStackErrorCaught.java:8) Try another one? Type your choice [y/n]: y 24 Finished IV The Try-throw-catch Mechanism The Try-throw-catch Mechanism: The block Tells what to do when everything goes smooth Can have code that throws s for unusual conditions If something goes wrong within the block, execution in the block is stoped and an is thrown The throw statement: We can write the throw statement to (create object and) throw an. The similar is done when JAVA detects problem (e.g., JAVA may throw an object of StackOverflowError). FileNotFound, InputMismatch, etc Syntax throw new ClassName(arguments); After is thrown, the flow of control is transferred to a catch block, and the catch block begins to run The catch block Tells what to do when an is caught One parameter, usually named e (but other names are also ok) Syntax catch (ClassName e) code to handle the Last modified: 8-Nov-2018 7/12

Throwable V Define our own classes Error IO Predefined class contains: - An object field to store a message and a constructor that sets the message - An accessor method, String getmessage() Runtime We define our own class by inheriting (or other throwables) We can add fields and methods in our class E.g. int problemvalue, int getproblemvalue() Constructors to be implemented: public class NegativeInteger extends private int problemvalue; public int getproblemvalue() return problemvalue; public NegativeInteger() super("negative integer!"); public NegativeInteger(String message) super(message); public NegativeInteger(String message, int v) super(message); problemvalue=v; Add constructor according to our design Which constructor will run is decided by how we create the object: throw new NegativeInteger(); It is customary to give both a default constructor and a constructor that contains a detailed message. throw new NegativeInteger("-ve number"); throw new NegativeInteger("-ve number", x); Example 6 Revised NegativeInteger: Add a field to store the problem value NegativeInteger: Inside main(): public class NegativeInteger extends private int problemvalue; public int getproblemvalue() return problemvalue; public NegativeInteger() super("negative integer!"); public NegativeInteger(String msg) super(msg); public NegativeInteger(String msg, int v) super(msg); problemvalue=v; if (x<0) throw new NegativeInteger( -ve number, x); catch (FileNotFound e) System.out.println("Cannot open. ); catch (InputMismatch e) System.out.println("Cannot read."); catch (NegativeInteger e) System.out.println(e.getMessage()+ " ["+e.getproblemvalue()+"]"); Throw? Throw? Throw! Rundown 6.1: Input the file pathname: c:\data003.txt -ve number [-1234] Last modified: 8-Nov-2018 8/12

VI Pitfall: Catch the more specific first When we have 2 or more catch-blocks, catch the more specific first Example 7 [based on Example 5] public static void processfile(string fname) throws FileNotFound, InputMismatch, NegativeInteger if (x<0) throw new NegativeInteger(); processfile(fname); catch (NegativeInteger e) System.out.println("Unexpected negative value from the file."); catch ( e) System.out.println("Some problem happens."); If you exchange their order, compiler complains: Unreachable catch block for NegativeInteger. It is already handled by the catch block for in.close(); Last modified: 8-Nov-2018 9/12

VII The throws clause, throws clause in derived class The Throws clause When a method may cause, but no catch is done within the method, Then it may inform the user by using the Throws clause to declare the The user may catch and handle the (or the user itself declares it again). Example 5 [recall] public static void processfile(string fname) throws FileNotFound, InputMismatch, NegativeInteger if (x<0) throw new NegativeInteger(); Inside main(): Declaring the The throws clause declares what s might occur. Throwing an. processfile(fname);. catch (FileNotFound e) catch (InputMismatch e) catch (NegativeInteger e). Handling an Propagation of s propagate through method calls in the stack until they are caught and handled. Java runtime system calls main() calls methoda() calls methodb() main() With handler methoda() Without handler methodb() Without handler Handle it and Continue to run Throws back Throws back s not handled finally will cause the program to abort. Java runtime system calls main() calls methoda() calls methodb() main() Without handler methoda() Without handler methodb() Without handler Program aborted by JAVA runtime Throws back Throws back Throws back Last modified: 8-Nov-2018 10/12

Throws clause in derived classes When we redefine a method in a subclass, it should have the same classes listed in its throws clause that it had in the superclass Or it should have a subset of them i.e., a subclass may not add any s to the throws clause, but it can delete some class ClassY public void processfile(string fname) throws FileNotFound Example 8 class ClassZ extends ClassY public void processfile(string fname) throws FileNotFound, NegativeInteger if (x<0) throw new NegativeInteger(); VIII The Catch-or-Declare Rule, Checked s, Unchecked s The Catch-or-Declare Rule If an may be thrown inside a method, the method may deal with it by 1. placing the concerned code within a -block, and handle in a catch-block, or 2. declaring it public void methodx(). //code which may throw catch ( e). //catch and handle the public void methodx() throws The compiler reinforces the Catch-or-Declare Rule on the following s [Known as checked s ]: For other s/errors, it is okay whether we deal with them or not. [Known as unchecked s ] Error Throwable Must Catch-or-Declare [ Checked s ] IO Runtime Last modified: 8-Nov-2018 11/12

IX The Finally Block The Finally Block The finally block contains code to be executed whether or not an is thrown in a block. Scanner in = new Scanner(System.in); System.out.print("Input the file pathname: "); String fname = in.next(); Example 6 Scanner infile=null; infile = new Scanner(new File(fname)); catch (FileNotFound e) System.out.println("Cannot open the file."); catch (InputMismatch e) System.out.println("Cannot read the required number."); catch (NegativeInteger e) System.out.println(e.getMessage()+" ["+e.getproblemvalue()+"]"); finally if (infile!= null) System.out.println("Closing infile"); else System.out.println("inFile not open"); in.close(); --- end --- Last modified: 8-Nov-2018 12/12