CS159. Nathan Sprague

Similar documents
CS159. Nathan Sprague

CSCI 261 Computer Science II

CSE 143 Java. Exceptions 1/25/

Programming II (CS300)

Errors and Exceptions

Programming II (CS300)

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

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 - Example. Exceptions - Example

Exception-Handling Overview

16-Dec-10. Consider the following method:

CS 3 Introduction to Software Engineering. 3: Exceptions

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:

Lecture 19 Programming Exceptions CSE11 Fall 2013

Exception Handling. Exception Handling

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

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

Exceptions and Design

Data Structures. 02 Exception Handling

C16b: Exception Handling

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

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

BBM 102 Introduction to Programming II Spring Exceptions

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

Comp 249 Programming Methodology Chapter 9 Exception Handling

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

CS61B Lecture #12. Today: Various odds and ends in support of abstraction.

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

Introduction to Computer Science II CS S-22 Exceptions

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

CS 251 Intermediate Programming Exceptions

Recreation. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Intro to Computer Science II. Exceptions

Programming Languages and Techniques (CIS120)

Fundamentals of Object Oriented Programming

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

For more details on SUN Certifications, visit

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

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

Fundamentos de programação

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

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Input-Output and Exception Handling

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

Chapter 12 Exception Handling

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

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

BBM 102 Introduction to Programming II Spring 2017

CS 11 java track: lecture 3

Exceptions. CSC207 Winter 2017

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

Object Oriented Programming

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

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

COMP-202 Unit 9: Exceptions

COMP-202 Unit 9: Exceptions

CP122 CS I. Chapter 11: File I/O and Exceptions

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

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

School of Informatics, University of Edinburgh

Midterm Exam CS 251, Intermediate Programming March 12, 2014

CS Programming I: Exceptions

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

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

EXCEPTIONS. Fundamentals of Computer Science I

Exceptions and Error Handling

Vendor: Oracle. Exam Code: 1Z Exam Name: Java SE 7 Programmer I. Version: Demo

Sri Vidya College of Engineering & Technology Question Bank

Correctness and Robustness

Exception Handling. Chapter 8. Chapter 8 1

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

Exception Handling. Chapter 9

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

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

CS159 Midterm #1 Review

Motivations. Chapter 12 Exceptions and File Input/Output

Chapter 13 Exception Handling

CSE 331 Software Design & Implementation

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

6.Introducing Classes 9. Exceptions

Exceptions and Libraries

What are Exceptions?

ITI Introduction to Computing II

CS111: PROGRAMMING LANGUAGE II

Defensive Programming

CSC 1214: Object-Oriented Programming

ITI Introduction to Computing II

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Building Java Programs

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

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

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

Here is a hierarchy of classes to deal with Input and Output streams.

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

CS S-22 Exceptions 1. Running a web server, don t want one piece of bad data to bring the whole thing down

Introduction to Programming Using Java (98-388)

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

CP122 CS I. Chapter 11: File I/O and Exceptions

Transcription:

CS159 Nathan Sprague

What s wrong with the following code? 1 /* ************************************************** 2 * Return the mean, or -1 if the array has length 0. 3 *************************************************** */ 4 public static double mean ( double [] numbers ) 5 { 6 double sum = 0; 7 double result ; 8 9 if ( numbers == null numbers. length == 0) 10 { 11 result = -1; 12 } 13 else 14 { 15 for ( int i = 0; i < numbers. length ; i++) 16 { 17 sum += numbers [i]; 18 } 19 result = sum / numbers. length ; 20 } 21 return result ; 22 }

Why Exceptions? Sometimes there is no appropriate return value that can be used to indicate an error has occurred. (Let s use exceptions to improve this code...)

Why Exceptions? Sometimes there is no appropriate return value that can be used to indicate an error has occurred. (Let s use exceptions to improve this code...) Exceptions provide flexibility in deciding where a particular problem should be handled. If an exception occurs, a method may: Pass the buck by using the throws keyword. The exception will be handled somewhere higher-up in the call stack. Deal with the exception using a try/catch block.

Fixed Mean Calculator 1 public static double mean ( double [] numbers ) 2 { 3 if ( numbers == null numbers. length == 0) 4 { 5 throw new IllegalArgumentException (" Invalid array."); 6 } 7 8 double sum = 0; 9 10 for ( int i = 0; i < numbers. length ; i++) 11 { 12 sum += numbers [i]; 13 } 14 15 return sum / numbers. length ; 16 }

throw vs. throws The throw keyword is similar to return Ends the method Sends a result (exception object) to the caller The throws keyword is similar to a method s return type specification. 1 public double somemethod ( double arg ) throws SomeCheckedException 2 { 3 if ( arg == 0.0) 4 { 5 throw new SomeCheckedException (" Bad argument."); 6 } 7 8 return 10.0; 9 }

Checked vs. Unchecked Exceptions The throws keyword is only required for checked exceptions They must be handled within the method with a try-catch block OR Declared thrown with throws (passing the buck) Unchecked exceptions inherit from RuntimeException Often result from programming errors, not exceptional circumstances.

Exception Class Hierarchy

try/catch 1 public double timesten ( double arg ) 2 { 3 double result = 0; 4 try 5 { 6 result = somemethod ( arg ) * 10.0; 7 } 8 catch ( SomeCheckedException e) 9 { 10 // Code here that fixes the problem... 11 // Maybe some other way of 12 // calculating a good result. 13 } 14 return result ; 15 }

throws 1 public double timesten ( double arg ) throws SomeCheckedException 2 { 3 return somemethod ( arg ) * 10.0; 4 }

Example... Let s write a Java program that reads a text file containing simple mathematical expressions, and writes the results: 2 + 3 22 / 2 3 * 2 5 11 6 If the input file contains: Terminal output should be:

What will be printed? 1 filename = " NONEXISTENTFILE. txt "; 2 System. out. print ("A "); 3 try 4 { 5 System. out. print ("B "); 6 file = new File ( filename ); 7 scanner = new Scanner ( file ); 8 System. out. print ("C "); 9 10 } 11 catch ( FileNotFoundException e) 12 { 13 System. out. print ("D "); 14 } 15 finally 16 { 17 System. out. print ("E "); 18 } 19 System. out. print ("F ");

Question Characterize the following code, assuming numbers is an array of doubles. (There are no syntax errors.) 1 double sum = 0; 2 try 3 { 4 for ( int i = 0; i <= numbers. length ; i++) 5 { 6 sum += numbers [i]; 7 } 8 } 9 catch ( ArrayIndexOutOfBoundsException e) 10 { 11 // Do nothing. 12 } 13 System. out. println ( sum ); 1 Correct result, appropriate use of exception handling. 2 Incorrect result, appropriate use of exception handling. 3 Correct result, inappropriate use of exception handling 4 Incorrect result, inappropriate use of exception handling.

Question Characterize the following code, assuming numbers is an array of doubles. (There are no syntax errors.) 1 double sum = 0; 2 try 3 { 4 for ( int i = 0; i <= numbers. length ; i++) 5 { 6 sum += numbers [i]; 7 } 8 } 9 catch ( ArrayIndexOutOfBoundsException e) 10 { 11 // Do nothing. 12 } 13 System. out. println ( sum ); 1 Correct result, appropriate use of exception handling. 2 Incorrect result, appropriate use of exception handling. 3 Correct result, inappropriate use of exception handling 4 Incorrect result, inappropriate use of exception handling. 3.

Handling vs. Throwing The specification for the following method indicates that it should throw an exception of type IllegalArgumentException if amount is less than 0. Is this implementation correct? 1 public void increasevalue ( int amount ) 2 { 3 try 4 { 5 if ( amount < 0) 6 { 7 throw new IllegalArgumentException (); 8 } 9 value += amount ; 10 } 11 catch ( IllegalArgumentException e) 12 { 13 System. out. println (" Negative amount not allowed!"); 14 } 15 }

Handling vs. Throwing The specification for the following method indicates that it should throw an exception of type IllegalArgumentException if amount is less than 0. Is this implementation correct? 1 public void increasevalue ( int amount ) 2 { 3 try 4 { 5 if ( amount < 0) 6 { 7 throw new IllegalArgumentException (); 8 } 9 value += amount ; 10 } 11 catch ( IllegalArgumentException e) 12 { 13 System. out. println (" Negative amount not allowed!"); 14 } 15 } No.