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

Similar documents
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

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

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

ECE 122. Engineering Problem Solving with Java

Check out FilesAndExceptions from SVN. Exam 2 Review File I/O, Exceptions Vector Graphics Project

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Object Oriented Programming Exception Handling

National University. Faculty of Computer Since and Technology Object Oriented Programming

Chapter 13 Exception Handling

Introduction to Software Design

COE318 Lecture Notes Week 10 (Nov 7, 2011)

BBM 102 Introduction to Programming II Spring Exceptions

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Exceptions - Example. Exceptions - Example

Object Oriented Programming

ITI Introduction to Computing II

Chapter 12 Exception Handling

ITI Introduction to Computing II

Exceptions in Java

Reminder. Topics CSE What Are Exceptions?! Lecture 11 Exception Handling

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

More on Exception Handling

C16b: Exception Handling

Exceptions. CSC207 Winter 2017

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Chapter 11 Handling Exceptions and Events. Chapter Objectives

Chapter 10. Exception Handling. Java Actually: A Comprehensive Primer in Programming

Programming II (CS300)

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

Introductory Programming Exceptions and I/O: sections

Lecture 19 Programming Exceptions CSE11 Fall 2013

More on Exception Handling

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

Exception Handling. Chapter 11. Java By Abstraction Chapter 11. Outline What Are Exceptions?

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

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

BBM 102 Introduction to Programming II Spring 2017

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

Exception Handling. Chapter 11. Outline. Example: The Quotient app What Are Exceptions? Java By Abstraction Chapter 11

Exception-Handling Overview

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

Agenda: Discussion Week 7. May 11, 2009

CSCI Object Oriented Design: Java Review Errors George Blankenship. Java Review - Errors George Blankenship 1

CSCI Object-Oriented Design. Java Review Topics. Program Errors. George Blankenship 1. Java Review Errors George Blankenship

CS 3 Introduction to Software Engineering. 3: Exceptions

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

Software Practice 1 - Error Handling

Programming II (CS300)

16-Dec-10. Consider the following method:

Fundamentals of Object Oriented Programming

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

Programming Languages and Techniques (CIS120)

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

CS 200 Command-Line Arguments & Exceptions Jim Williams, PhD

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

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

EXCEPTION HANDLING. Summer 2018

Introduction to Computer Science II CS S-22 Exceptions

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

CSC 1214: Object-Oriented Programming

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

Recitation 3. 2D Arrays, Exceptions

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

Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani. Exception Handling

Exception Handling. Exception Handling

COMP1008 Exceptions. Runtime Error

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

Exceptions and Error Handling

Exceptions Handling Errors using Exceptions

File I/O and Exceptions

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Introduction to Java. Handout-3a. cs402 - Spring

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

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

Exceptions. What exceptional things might our programs run in to?

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

Object-Oriented Programming Design Topic : Exception Programming

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

CS 251 Intermediate Programming Exceptions

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

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

Defensive Programming. Ric Glassey

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

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

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

Object Oriented Programming

CS159. Nathan Sprague

Exception in thread "main" java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling.

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

Typecasts and Dynamic Dispatch. Dynamic dispatch

Motivations. Chapter 12 Exceptions and File Input/Output

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

Exceptions and assertions

17. Handling Runtime Problems

CS11 Java. Fall Lecture 4

Transcription:

Administration Exceptions CS 99 Summer 2000 Michael Clarkson Lecture 11 Lab 10 due tomorrow No lab tomorrow Work on final projects Remaining office hours Rick: today 2-3 Michael: Thursday 10-noon, Monday 10-noon Final projects due Tuesday by noon via FTP You will receive your final grade from Cornell, not me 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 2 Leftovers Exceptions Course evaluations Agenda Leftovers Topics we didn t get to cover: Multi-dimensional arrays Advanced String operations Packages Using the debugger Files 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 3 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 4 When Things Go Wrong In the real world: Input isn t nicely formatted Hardware devices fail Memory is limited Code you rely on is buggy What to do? Give up and abort the program Return codes Exceptions 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 5 Handling Errors Abort: Calculator program, given division by 0 Poor choice, should at least: Return to safe state and enable user to execute other commands, or: Allow user to save work and terminate program gracefully Data validation: Most any lab since we learned loops What if you can t reinput a value, though? Example: user calls a method with illegal parameters 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 6 1

Return Codes Return code: value returned from a method indicating success or failure Usually a boolean or integer If integer, constants often defined Example: Time class Needed to settime, but parameters could be invalid Our solution: use default values Another solution: use return codes Return Codes [2] boolean settime(int h, int m, int s) { boolean success; if (isvalidhour(h) && isvalidmin(m) && isvalidsec(s)) { hour = h; min = m; sec = s; success = true; else { success = false; return success; 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 7 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 8 Return Codes [3] // input h, m, s from user boolean ret = t.settime(h, m, s); if (ret) { // continue with program else { // reinput h, m, s and try again Return Codes [4] Problems: What if we also want to return something in addition to the return code? Users of the method can ignore return code. Can t use for constructors Doesn t fit OOP model to return numbers/booleans when we re really trying to indicate an error 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 9 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 10 Exceptions Used to handle exceptional situations in code Instead of returning normally, a method can throw an object containing information about the situation (error) That object is an exception Method exits immediately after throwing an exception Does not return normally (return any value) Does not return to where method was called Example Exception class DivBy0 { public static void main(string[] args) { int num = 10, den = 0; System.out.println(num / den); System.out.println( Will never get here ); Exception in thread main java.lang.arithmeticexception: / by zero at Zero.main(Zero.java:4) 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 11 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 12 2

Exception Output Type of exception java.lang.arithmeticexception Descriptive message / by 0 Stack trace List of methods that had been called to get to that point in the program where the exception occured Zero.main(Zero.java:4) 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 13 Handling Exceptions Users would rather see a nice error message, not exception output Can prevent output of exception by using a trycatch block: // code that could throw an exception catch (ExceptionType objectname) { // code to execute if exception occurs 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 14 Exception Example, revisited class DivBy0 { public static void main(string[] args) { int num = 10, den = 0; System.out.println(num / den); catch (ArithmeticException e) { System.out.println( You can t divide by + zero! ); System.out.println( Will always get here ); There s always a catch Every try block must have at least one catch Can be multiple catch clauses: catch () { catch () { Each clause must catch a different type of exception The try block must be able to throw each type of exception 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 15 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 16 finally After all the catch clauses can be a single finally clause: catch () { finally { finally code is executed after everything else 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 17 Flow of Control Exception doesn t occur: Entire try clause is executed No catch clause is executed Finally clause is executed After try block, execution proceeds with first statement after catch/finally clauses Exception occurs: As soon as it occurs, control jumps to the single appropriate catch clause (exception handler) After clause is executed, execution proceeds with finally, then the first statement after catch/finally clauses 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 18 3

Causes of Exceptions 1. Calling a method that can throw an exception 2. Detecting an error and throwing an exception yourself 3. Making a programming error, such as a[-1]=0 4. An internal error occurs in Java Partial Exception Hierarchy Object Throwable Exception Error ArithmeticException IndexOutOfBoundsException IOException TooManyListenersException VirtualMachineError 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 19 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 20 Checked vs. Unchecked A checked exception is checked by the compiler to make sure you catch it All subclasses of Exception other than An unchecked exception is not checked by the compiler you don t have to catch it All subclasses of Error and Advertising Exceptions Every method must declare the checked exceptions it can throw: void mymethod() throws IOException, { Doesn t have to declare: Unchecked exceptions Exceptions it catches itself main: any exceptions 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 21 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 22 Exception Propagation Exceptions propagate up the call stack until they are caught So if one method doesn t catch an exception, Java checks the method that called it for a handler, all the way up to main If no handler can be found, the default error message is printed and the program aborts Propagation Example Exception generated by ExceptionProp.level3() No handler in level3 No handler in level2 Handler in level1 What is the output? 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 23 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 24 4

Propagation Example [2] Program beginning. Level 1 beginning. Level 2 beginning. Level 3 beginning. Arithmetic exception occurred. Level 1 ending. Program ending. Throwing Exceptions You can throw exceptions yourself using the throw statement: throw exceptionreference; You can get a reference to an exception by creating one: ArithmeticException e = new ArithmeticException( message ); throw e; 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 25 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 26 settime, revisited Instead of default values or return code, throw an exception IllegalArgumentException is a used to indicate illegal values were passed to a method Since unchecked, responsibility is on programmer to make sure illegal values not passed to constructor or settime settime, revisited [2] public Time(int h, int m, int s) { settime(h, m, s); public void settime(int h, int m, int s) { if (isvalidhour(h) && isvalidmin(m) && isvalidsec(s)) { hour = h; min = m; sec = s; else { throw new IllegalArgumentException( Hour, + minute, or second was illegal ); 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 27 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 28 Creating Exceptions Course Evaluations Can create your own types of exceptions by declaring classes that extend Exception Extremely useful for handling errors in large programs Add {fields, methods to exception to {store data, take action on errors 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 29 8/2/00 CS 99 Summer 2000 Michael Clarkson Lecture 11 30 5