Why Exceptions? (1.2) Exceptions. Why Exceptions? (1.1) Why Exceptions? (2.1)

Similar documents
CS 111 Classes I 1. Software Organization View to this point:

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

D06 PROGRAMMING with JAVA

What are exceptions? Bad things happen occasionally

Introduction to Java

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Lecture J - Exceptions

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Using Files as Input/Output in Java 5.0 Applications

Creating a Simple, Multithreaded Chat System with Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Exception Handling In Web Development DevelopIntelligence LLC

AP Computer Science Java Subset

Inheritance, overloading and overriding

Explain the relationship between a class and an object. Which is general and which is specific?

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

WRITING DATA TO A BINARY FILE

Chapter 2 Introduction to Java programming

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Software Engineering Techniques

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Topic 11 Scanner object, conditional execution

Part I. Multiple Choice Questions (2 points each):

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Sample CSE8A midterm Multiple Choice (circle one)

Building Java Programs

Java Interview Questions and Answers

AP Computer Science File Input with Scanner

CS506 Web Design and Development Solved Online Quiz No. 01

No no-argument constructor. No default constructor found

Software Construction

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Basics of Java Programming Input and the Scanner class

Question R11.3. R11.3 How do you open a file whose name contains a backslash, like c:\temp\output.dat?

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Introduction to Java. CS 3: Computer Programming in Java

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Java Application Developer Certificate Program Competencies

Building Java Programs

CS193j, Stanford Handout #10 OOP 3

public static void main(string[] args) { System.out.println("hello, world"); } }

Object-Oriented Programming in Java

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

AP Computer Science Static Methods, Strings, User Input

Copyright. Restricted Rights Legend. Trademarks or Service Marks. Copyright 2003 BEA Systems, Inc. All Rights Reserved.

CSE 1020 Introduction to Computer Science I A sample nal exam

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Building Java Programs

Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;

Java Programming Language

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

public static void main(string args[]) { System.out.println( "f(0)=" + f(0));

Line-based file processing

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Continuous Integration Part 2

Pemrograman Dasar. Basic Elements Of Java

Security Module: SQL Injection

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Logging in Java Applications

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Event-Driven Programming

7 Mutating Object State

Introduction to Programming

D06 PROGRAMMING with JAVA

core 2 Basic Java Syntax

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

AP Computer Science Java Mr. Clausen Program 9A, 9B

Course Intro Instructor Intro Java Intro, Continued

Smallest Java Package? Java.applet.* having 1 class and 3 interfaces. Applet Class and AppletContext, AppletStub, Audioclip interfaces.

Object Oriented Software Design

Chapter 13 - Inheritance

Crash Course in Java

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Install Java Development Kit (JDK) 1.8

Comp 248 Introduction to Programming

Topics in Software Reliability

J a v a Quiz (Unit 3, Test 0 Practice)

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Java Programming Fundamentals

CS 1302 Ch 19, Binary I/O

13 File Output and Input

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

MR-(Mapreduce Programming Language)

Transcription:

Exceptions Readings: IJP Chapter 12 CSE526: Principles of Programming Languages Spring 2016 CHEN-WEI WANG Why Exceptions? (1.2) 1 class CircleCalculator { 2 public static void main(string[] args) { 3 Circle c = new Circle(); 4 c.setradius(-10); 5 double area = c.getarea(); 6 System.out.println("Area: " + area); 7 8 A negative radius is passed to setradius in Line 4. The execution always flows smoothly from Lines 4 to Line 5, even when there was an error message printed from Line 4. It is not feasible to check if there is any kind of error message printed to the console right after the execution of Line 4. Solution: A way to let the caller of setradius realize that things might go wrong. ) When things do go wrong, immediate actions are needed. 3 of 29 Why Exceptions? (1.1) class Circle { double radius; Circle() { /* radius defaults to 0 */ void setradius(double r) { if (r < 0) { System.out.println("Error: negative radius."); else { radius = r; double getarea() { return radius * radius * 3.14; A negative radius is considered as an invalid input value to method setradius. When an error occurs, a message is printed to the console to warn the caller of setradius. However, when an error occurs, the caller of setradius may or may not see the error message in the console. ) It impossible to always force the caller setradius to stop on, or to handle, invalid input values. 2 of 29 Why Exceptions? (2.1) class Account { int id; double balance; Account() { /* balance defaults to 0 */ void deposit(double a) { if (a < 0) { System.out.println("Error: negative deposit."); else { balance += a; void withdraw(double a) { if (a < 0 balance - a < 0) { System.out.println("Error: invalid withdraw."); else { balance -= a; A negative deposit or withdraw amount is invalid. When an error occurs, a message is printed to the console. It is impossible to force the caller of deposit or withdraw to stop on, or to handle, invalid input values. 4 of 29

Why Exceptions? (2.2) class Bank { Account[] accounts; int numberofaccounts; Account(int id) { void withdraw(int id, double a) { for(int i = 0; i < numberofaccounts; i ++) { if(accounts[i].id == id) { if (a < 0 accounts[i].balance - a < 0) { System.out.println("Error: invalid withdraw."); else { accounts[i].withdraw(a); /* end for */ /* end withdraw */ The same error checking on input a is duplicated. It is impossible to force the caller of withdraw to stop on, or to handle, invalid input values. 5 of 29 Exceptions in Java (1.1) public class InvalidRadiusException extends Exception { public InvalidRadiusException(String s) { super(s); A new kind of Exception: InvalidRadiusException For any method that can have this kind of error, we declare at that method s signature that it may throw an InvalidRaidusException object. 7 of 29 Why Exceptions? (2.3) class BankApplication { pubic static void main(string[] args) { Bank b = new Bank(); Account acc1 = new Account(23); b.addaccount(acc1); Scanner input = new Scanner(System.in); double a = input.nextdouble(); if(a < 0 acc.balance - a < 0) { System.out.println("Error: invalid withdraw."); else { b.withdraw(23, a); System.out.println(acc1.balance); BankApplication uses Bank, which uses Account. The actual update of balance occurs at the level of Account. Error checking of withdraw amount is duplicated at all levels! Solution: Define error checking only once and let it propagate. 6 of 29 Exceptions in Java (1.2) class Circle { double radius; Circle() { /* radius defaults to 0 */ void setradius(double r) throws InvalidRadiusException { if (r < 0) { throw new InvalidRadiusException("Negative radius."); else { radius = r; double getarea() { return radius * radius * 3.14; As part of the signature of setradius, we declare that it may throw an InvalidRadiusException object at runtime. Any method that calls setradius will be forced to deal with this potential error. 8 of 29

Exceptions in Java (1.3) 1 class CircleCalculator { 2 public static void main(string[] args) { 3 Circle c = new Circle(); 4 try { 5 c.setradius(-10); 6 double area = c.getarea(); 7 System.out.println("Area: " + area); 8 9 catch(invalidradiusexception e) { 10 System.out.println(e); 11 12 Lines 6 is forced to be wrapped within a try-catch block, since it may throw an InvalidRadiusException object. If an InvalidRadiusException object is thrown from Line 6, then the normal flow of execution is interrupted and we go to the catch block starting from Line 9. 9 of 29 Exceptions in Java (2.2) class Account { int id; double balance; Account() { /* balance defaults to 0 */ void withdraw(double a) throws InvalidTransactionException { if (a < 0 balance - a < 0) { throw new InvalidTransactionException("Invalid withdraw."); else { balance -= a; As part of the signature of withdraw, we declare that it may throw an InvalidTransactionException object at runtime. Any method that calls withdraw will be forced to deal with this potential error. 11 of 29 Exceptions in Java (2.1) public class InvalidTransactionException extends Exception { public InvalidTransactionException(String s) { super(s); A new kind of Exception: InvalidTransactionException For any method that can have this kind of error, we declare at that method s signature that it may throw an InvalidTransactionException object. Exceptions in Java (2.3) class Bank { Account[] accounts; int numberofaccounts; Account(int id) { void withdraw(int id, double a) throws InvalidTransactionException { for(int i = 0; i < numberofaccounts; i ++) { if(accounts[i].id == id) { accounts[i].withdraw(a); /* end for */ /* end withdraw */ As part of the signature of withdraw, we declare that it may throw an InvalidTransactionException object. Any method that calls withdraw will be forced to deal with this potential error. We are propagating the potential error for the right party (i.e., BankApplication) to handle. 10 of 29 12 of 29

Exceptions in Java (2.4) 1 class BankApplication { 2 pubic static void main(string[] args) { 3 Bank b = new Bank(); 4 Account acc1 = new Account(23); 5 b.addaccount(acc1); 6 Scanner input = new Scanner(System.in); 7 double a = input.nextdouble(); 8 try { 9 b.withdraw(23, a); 10 System.out.println(acc1.balance); 11 catch (InvalidTransactionException e) { 12 System.out.println(e); Lines 9 is forced to be wrapped within a try-catch block, since it may throw an InvalidTransactionException object. If an InvalidTransactionException object is thrown from Line 9, then the normal flow of execution is interrupted and we go to the catch block starting from Line 11. 13 of 29 Stack of Method Calls Execution of a Java project starts from the main method of some class (e.g., CircleTester, BankApplication). Each line of method call involves the execution of that method s body implementation That method s body implementation may also involve method calls, which may in turn involve more method calls, and etc. It is typical that we end up with a chain of method calls! We call this chain of method calls a call stack. For example: Account.withdraw [top of stack; latest called] Bank.withdraw BankApplication.main [bottom of stack; earliest called] The closer a method is to the top of the call stack, the later its call was made. 15 of 29 What is an Exception? What to Do When an Exception Is Thrown? (1) An exception is an event, which occurs during the execution of a program disrupts the normal flow of the program s instructions When an error occurs within a method: the method throws an exception: first creates an exception object then hands it over to the runtime system the exception object contains information about the error: type [e.g., NegativeRadiusException] the state of the program when the error occurred throws an exception forwards/ propagates an exception catches an exception Method where error occurred and an exception object thrown (top of call stack) Method without an exception handler Method with an exception handler method call method call method call main method (bottom of call stack) 14 of 29 16 of 29

What to Do When an Exception Is Thrown? (2) After a method throws an exception, the runtime system searches the corresponding call stack for a method that contains a block of code to handle the exception. This block of code is called an exception handler. An exception handler is appropriate if the type of the exception object thrown matches the type that can be handled by the handler. The exception handler chosen is said to catch the exception. The search goes from the top to the bottom of the call stack: The method in which the error occurred is searched first. The exception handler is not found in the current method being searched ) Search the method that calls the current method, and etc. When an appropriate handler is found, the runtime system passes the exception to the handler. The runtime system searches all the methods on the call stack without finding an appropriate exception handler ) The program terminates and the exception object is directly thrown to the console! 17 of 29 The Catch or Specify Requirement (2) Code (e.g., a method call) that might throw certain exceptions must be enclosed by one of the two ways: 2. The Specify Solution: A method that specifies as part of its signature that it can throw the exception. 19 of 29 class Bank { void withdraw (double amount) throws InvalidTransactionException { accounts[i].withdraw(amount); The Catch or Specify Requirement (1) Code (e.g., a method call) that might throw certain exceptions must be enclosed by one of the two ways: 1. The Catch Solution: A try statement that catches and handles the exception. 18 of 29 main() { Circle c = new Circle(); try { c.setradius(-10); catch(negativeraidusexception e) { The Catch or Specify Requirement (3) There are three basic categories of exceptions 20 of 29 VirtualMachineError OutofMemoryError Error IOError IndexOutOfBoundsException ArrayIndexOutOfBoundsException Throwable IllegalArgumentException NumberFormatException RuntimeException NoSuchElementException Exception FileNotFoundException NullPointerException Only one category of exceptions is subject to the Catch or Specify Requirement. IOException EOFException ClassCastException

Exception Category (1): Checked Exceptions Checked exceptions are exceptional conditions that a well-written application should anticipate and recover from. An application prompts a user for a circle radius, a deposit/withdraw amount, or the name of a file to open. Normally, the user enters a positive number for radius/deposit, a not-too-big positive number for withdraw, and existing file to open. When the user enters invalid numbers or file names, NegativeRadiusException, InvalidTransactionException, or FileNotFoundException is thrown. A well-written program will catch this exception and notify the user of the mistake. Checked exceptions are: subject to the Catch or Specify Requirement. subclasses of Exception that are not descendant classes of Error or RuntimeException. 21 of 29 Exception Category (3): Runtime Exceptions Runtime exceptions are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API. e.g., NullPointerException e.g., ClassCastException e.g., ArrayIndexOutOfBoundException Runtime exceptions are: not subject to the Catch or Specify Requirement. subclasses of RuntimeException Errors and Runtime exceptions are collectively known as unchecked exceptions. 23 of 29 Exception Category (2): Errors Errors are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. An application successfully opens a file for input. But the file cannot be read because of a hardware or system malfunction. The unsuccessful read will throw java.io.ioerror Errors are: 22 of 29 not subject to the Catch or Specify Requirement. subclasses of Error Catching and Handling Exceptions To construct an exception handler : 1. Enclose the code that might throw an exception within a try block. 2. Associate each possible kind of exception that might occur within the try block with a catch block. 3. Append an optional finally block. try { /* code that might throw exceptions */ catch(exceptiontype1 e) { catch(exceptiontype2 e) { finally { When an exception is thrown from Line i in the try block: Normal flow of execution is interrupted: the rest of try block starting from Line i + 1 is skipped. Each catch block performs an instanceof check on the thrown exception: the first matched catch block is executed. The finally block is always executed after the matched catch 24 of 29block is executed.

Examples (1) double r = ; double a = ; try{ Bank b = new Bank(); b.addaccount(new Account(34)); b.deposit(34, a) Circle c = new Circle(); c.setradius(r); System.out.println(r.getArea()); catch(negativeradiusexception e) { System.out.println(r + " is not a valid radius value."); catch(invalidtransactionexception e) { System.out.println(r + " is not a valid transaction value."); catch( Exception e) { /* any other kinds of exceptions */ 25 of 29 Exercise (3.1) The Integer class supports a method for parsing Strings: public static int parseint(string s) throws NumberFormatException e.g., Integer.parseInt("23") returns 23 e.g., Integer.parseInt("twenty-three") throws a NumberFormatException Write a fragment of code that prompts the user to enter a string (using nextline from Scanner) that represents an integer. If the user input is not a valid integer, then prompt them to enter again. 27 of 29 Examples (2): Problem? double r = ; double a = ; try{ Bank b = new Bank(); b.addaccount(new Account(34)); b.deposit(34, a) Circle c = new Circle(); c.setradius(r); System.out.println(r.getArea()); catch( Exception e) { catch(negativeradiusexception e) { System.out.println(r + " is not a valid radius value."); catch(invalidtransactionexception e) { System.out.println(r + " is not a valid transaction value."); 26 of 29 Exercise (3.2) Scanner input = new Scanner(System.in); boolean validinteger = false; while (!validinteger) { System.out.println("Enter an integer:"); String userinput = input.nextline(); try { int userinteger = Integer.parseInt(userInput); validinteger = true; catch(numberformatexception e) { System.out.println(userInput + " is not a valid integer."); /* validinteger remains false */ 28 of 29

Index (1) Why Exceptions? (1.1) Why Exceptions? (1.2) Why Exceptions? (2.1) Why Exceptions? (2.2) Why Exceptions? (2.3) Exceptions in Java (1.1) Exceptions in Java (1.2) Exceptions in Java (1.3) Exceptions in Java (2.1) Exceptions in Java (2.2) Exceptions in Java (2.3) Exceptions in Java (2.4) What is an Exception? Stack of Method Calls 29 of 29 Index (2) What to Do When an Exception Is Thrown? (1) What to Do When an Exception Is Thrown? (2) The Catch or Specify Requirement (1) The Catch or Specify Requirement (2) The Catch or Specify Requirement (3) Exception Category (1): Checked Exceptions Exception Category (2): Errors Exception Category (3): Runtime Exceptions Catching and Handling Exceptions Examples (1) Examples (2): Problem? Exercise (3.1) Exercise (3.2) 30 of 29