EXCEPTIONS. Fundamentals of Computer Science I

Similar documents
Excep&ons and file I/O

Building Java Programs

Building Java Programs

CSCI 1103: File I/O, Scanner, PrintWriter

Building Java Programs

AP Computer Science. File Input with Scanner. Copyright 2010 by Pearson Education

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

C16b: Exception Handling

Computer Science is...

Exception-Handling Overview

CS159. Nathan Sprague

CSCI 1103: File I/O, Scanner, PrintWriter

BBM 102 Introduction to Programming II Spring Exceptions

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

Introduction to Programming Using Java (98-388)

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

A token is a sequence of characters not including any whitespace.

CIS 110: Introduction to Computer Programming

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

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

More on Exception Handling

More on Exception Handling

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

COMP-202 Unit 9: Exceptions

GRAPHICS AND SOUND. Fundamentals of Computer Science I

Lecture 19 Programming Exceptions CSE11 Fall 2013

Reading Input from Text File

COMP-202 Unit 9: Exceptions

CSCI 261 Computer Science II

File Processing. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. A Class for Representing a File

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

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

Input-Output and Exception Handling

Unit 10: exception handling and file I/O

BBM 102 Introduction to Programming II Spring 2017

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

Errors and Exceptions

Computational Expression

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

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

CS159. Nathan Sprague

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

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

ITI Introduction to Computing II

Reading Text Files. 1 Reading from text files: Scanner

ITI Introduction to Computing II

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

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

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

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

Example Program. public class ComputeArea {

Object Oriented Programming Exception Handling

CS Programming I: Exceptions

Exceptions and Error Handling

Building Java Programs Chapter 6

9. Java Errors and Exceptions

Remedial Java - Excep0ons 3/09/17. (remedial) Java. Jars. Anastasia Bezerianos 1

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

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

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

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

Comp 249 Programming Methodology Chapter 9 Exception Handling

CS 112 Introduction to Programming

Exceptions - Example. Exceptions - Example

CS244 Advanced Programming Applications

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

Building Java Programs

Defensive Programming

CS Programming I: Exceptions

CS 200 File Input and Output Jim Williams, PhD

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

Objektorienterad programmering

COMP 401 EXCEPTIONS. Instructor: Prasun Dewan

Introduction to Computation and Problem Solving. Class 25: Error Handling in Java. Prof. Steven R. Lerman and Dr. V. Judson Harward.

Exceptions Handling Errors using Exceptions

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

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

Chapter 12 Exception Handling and Text IO. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

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

Typecasts and Dynamic Dispatch. Dynamic dispatch

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

Outline. CIS 110: Introduction to Computer Programming. Exam announcements. Programming assertions revisited. An extended example: mystery

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

Data Structures. 02 Exception Handling

Java Errors and Exceptions. Because Murphy s Law never fails

COMP102: 181. Menu. More while loops. Admin: Test. Peter Andreae

Building Java Programs

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

CS159 Midterm #1 Review

CS 3 Introduction to Software Engineering. 3: Exceptions

What are Exceptions?

Fundamentals of Object Oriented Programming

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

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

Lecture 5: Methods CS2301

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

Transcription:

EXCEPTIONS Exception in thread "main" java.lang.numberformatexception: For input string: "3.5" at java.lang.numberformatexception.forinputstring(numberformatexception.java:48) at java.lang.integer.parseint(integer.java:458) at java.lang.integer.parseint(integer.java:499) at AddNums.main(AddNums.java:8) Fundamentals of Computer Science I

Outline Exceptions An important part of writing defensive code Defending against bad input Handling unexpected events e.g. File is missing e.g. Trying to parse "$56.89" as a double Many Java classes require exception handling e.g. File I/O, network communication, playing sounds, using threads Checked versus unchecked exceptions

Adding Two Numbers Goal: Defend against all types of bad input Problem 1: Crashes if less than 2 arguments public class AddNums public static void main(string [] args) int num1 = Integer.parseInt(args[0]); int num2 = Integer.parseInt(args[1]); int sum = num1 + num2; System.out.println(num1 + " + " + num2 + " = " + sum); % java AddNums Exception in thread "main" java.lang.arrayindexoutofboundsexception: 0 at AddNums.main(AddNums.java:5) 3

Adding Two Numbers Goal: Defend against all types of bad input Fix 1: Add conditional to check there are 2 args Problem 2: Crashes if passed a non-integer arg public class AddNums public static void main(string [] args) if (args.length < 2) System.out.println("AddNums <integer 1> <integer 2>"); return; int num1 = Integer.parseInt(args[0]); int num2 = Integer.parseInt(args[1]); int sum = num1 + num2; System.out.println(num1 + " + " + num2 + " = " + sum); % java AddNums 2 3.5 Exception in thread "main" java.lang.numberformatexception: For input string: "3.5" at java.lang.numberformatexception.forinputstring(numberformatexception.java:48) at java.lang.integer.parseint(integer.java:458) at java.lang.integer.parseint(integer.java:499) at AddNums.main(AddNums.java:8) 4

Adding Two Numbers How to check for invalid input to parseint? e.g. 1.0, 192.168.1.4, $1, 123., one public class AddNums public static void main(string [] args) if (args.length < 2) System.out.println("AddNums <integer 1> <integer 2>"); return; int num1 = Integer.parseInt(args[0]); int num2 = Integer.parseInt(args[1]); int sum = num1 + num2; System.out.println(num1 + " + " + num2 + " = " + sum); 5

Java Exceptions When things go wrong: Java throws an exception An exception is an object that can be caught You get to decide if program can recover or not Rather than always crashing with a runtime error % java AddNums 2 3.5 Exception in thread "main" java.lang.numberformatexception: For input string: "3.5" at java.lang.numberformatexception.forinputstring(numberformatexception.java:48) at java.lang.integer.parseint(integer.java:458) at java.lang.integer.parseint(integer.java:499) at AddNums.main(AddNums.java:8) 6

try-catch Block try // Do some risky things // Do some more risky things catch (Exception e) // Try and recover from the problem The mother of all exception types. All other types of exceptions inherit from this parent class. If something goes horribly wrong on a line in the try block, flow of control immediately jumps to the catch block. Details about the exception are passed as an object into the catch block. Like a method s parameter list, e is just a name for the Exception object in the catch block. 7

Add Code to catch all Exceptions public class AddNums public static void main(string [] args) try int num1 = Integer.parseInt(args[0]); int num2 = Integer.parseInt(args[1]); int sum = num1 + num2; System.out.println(num1 + " + " + num2 + " = " + sum); catch (Exception e) System.out.println("Something went wrong!"); System.out.println("End of program"); % java AddNums 2 3.5 Something went wrong! End of program % java AddNums Something went wrong! End of program Not an ideal solution: How is the user suppose to know what to do differently? 8

A Better Solution public static void main(string [] args) if (args.length < 2) System.out.println("AddNums <integer 1> <integer 2>"); return; int num1, num2; try num1 = Integer.parseInt(args[0]); catch (NumberFormatException e) System.out.println("1st argument invalid: " + args[0]); return; try num2 = Integer.parseInt(args[1]); catch (NumberFormatException e) System.out.println("2nd argument invalid: " + args[1]); return; int sum = num1 + num2; System.out.println(num1 + " + " + num2 + " = " + sum); Principle 1: Don't catch exceptions you can handle with logic such as staying in bounds of an array. scoping rules apply inside a tryblock: You'll need to declare variables outside if you need them in catch-block or later. Principle 2: Don't use generic Exception class. Catch the specific type of exception you had in mind. 9

Reading a Text File Need two Java classes: File class, represents a filename System independent abstraction of a file s path Not actually used for reading or writing Scanner class, parses out values 10

Handy Methods: File Class Method Description boolean canread() boolean canwrite() boolean delete() boolean exists() long length() String getname() String getpath() boolean mkdir() Test if the program can read from the file. Test if the program can write to the file. Attempt to delete the file. See if the given file exists. Get length of the file in bytes Returns the filename portion of the path Returns the path without the filename Creates a directory specified by the path File file = new File("c:\\workspace\\nums.txt"); System.out.println(file.getName()); System.out.println(file.getPath()); Windows paths need escaping of the backslash by using two of them. nums.txt c:\workspace 11

Handy Methods: Scanner Class Method String next() String nextline() int nextint() double nextdouble() Description Returns the next string, separated via whitespace Returns the entire line up to the next line break Returns the next integer Returns the next double boolean hasnext() boolean hasnextline() boolean hasnextint() boolean hasnextdouble() Are there any more tokens available? Is there another line available? Can the next token be interpreted as an int? Can the next token be interpreted as a double? void close() Free up resources 12

Averaging Numbers public class AvgNumsFile public static void main(string [] args) double sum = 0.0; long count = 0; if (args.length < 1) System.out.println("AvgNumsFile <filename>"); return; try Scanner scanner = new Scanner(new File(args[0])); while (scanner.hasnext()) sum += scanner.nextdouble(); count++; scanner.close(); System.out.println(sum / count); catch (FileNotFoundException e) System.out.println("Failed to open file!"); New program: reads from filename given by args[0]. % java AvgNumsFile squares.txt 332833.5 13

Trying to Break it public class AvgNumsFile public static void main(string [] args) double sum = 0.0; long count = 0; if (args.length < 1) % java AvgNumsFile noexist.txt Failed to open file! % java AvgNumsFile mobydick.txt Exception in thread "main" java.util.inputmismatchexception System.out.println("AvgNumsFile <filename>"); at java.util.scanner.throwfor(scanner.java:840) return; at java.util.scanner.next(scanner.java:1461) at java.util.scanner.nextdouble(scanner.java:2387) try at AvgNumsFile.main(AvgNumsFile.java:21) Scanner scanner = new Scanner(new File(args[0])); while (scanner.hasnext()) sum += scanner.nextdouble(); count++; scanner.close(); System.out.println(sum / count); catch (FileNotFoundException e) System.out.println("Failed to open file!"); 14

Multiple catch Blocks... double sum = 0.0; long count = 0; if (args.length < 1) System.out.println("AvgNumsFile <filename>"); return; try Scanner scanner = new Scanner(new File(args[0])); while (scanner.hasnext()) % java AvgNumsFile mobydick.txt sum += scanner.nextdouble(); Invalid data in file! count++; scanner.close(); System.out.println(sum / count); catch (FileNotFoundException e) System.out.println("Failed to open file!"); catch (InputMismatchException e) System.out.println("Invalid data in file!");... 15

Throwing Exceptions How do exceptions start their life? Somebody throws them: throw new NoCaffeineException(); Whoever is using the method has to catch Or else that method can throw it What if nobody catches it? Checked exceptions Causes compile error if exception is not caught Unchecked exceptions Will compile without a catch, catching is optional Usually a programming error e.g. ArrayIndexOutOfBoundsException, NullPointerException 16

Java Exception Class Hierarchy http://www.faqs.org/docs/javap/c9/s3.html 17

Finally, the finally Block Finally block executes no matter what If no exception, runs after try-block If exception occurs, runs after catch-block Useful for doing cleanup that is always needed try turnovenon(); x.bake(); catch (BakingException e) e.printstacktrace(); finally turnovenoff(); Prints out the stack trace (like what you see when you get a runtime error). Handy for debugging what line in the try-block is causing the trouble. 18

Summary Exceptions An important part of writing defensive code Defending against bad input Handling unexpected events e.g. File is missing e.g. Trying to parse "$56.89" as a double Many Java classes require exception handling e.g. File I/O, network communication, playing sounds, using threads Checked versus unchecked exceptions

Try It! This is not really an extra credit activity, but what I want you to do is download the examples from today along with the text files. Particularly with the AvgNumsFile.java code, try running it with both text files and see what happens. You can even create your own text file with numbers in it and use it on the command line instead. For example, you could put the numbers: 1 2 3 in a file and see if the program averages them out to 2.0 correctly.