Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Similar documents
Lecture 16: HashTables 10:00 AM, Mar 2, 2018

Lecture 5: Implementing Lists, Version 1

Errors and Exceptions

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

Exceptions in Java

Lecture 22: Garbage Collection 10:00 AM, Mar 16, 2018

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

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

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:

Program Correctness and Efficiency. Chapter 2

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

Designing Robust Classes

CS159. Nathan Sprague

Lab 4: Imperative & Debugging 12:00 PM, Feb 14, 2018

Lab 5: Java IO 12:00 PM, Feb 21, 2018

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

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

Lecture 7: Implementing Lists, Version 2

Good Coding Practices Spring 2018

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

COMP1008 Exceptions. Runtime Error

CS112 Lecture: Exceptions and Assertions

The compiler is spewing error messages.

Programming II (CS300)

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Unit Testing as Hypothesis Testing

Assertions and Exceptions Lecture 11 Fall 2005

Checked and Unchecked Exceptions in Java

BBM 102 Introduction to Programming II Spring Exceptions

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

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

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

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

Object Oriented Programming. Week 7 Part 1 Exceptions

BBM 102 Introduction to Programming II Spring 2017

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

Lecture 2: SML Basics

Lecture 8: Iterators and More Mutation

Lab 10: Sockets 12:00 PM, Apr 4, 2018

Internal Classes and Exceptions

Programming II (CS300)

CS2102: Lecture on Abstract Classes and Inheritance. Kathi Fisler

In fact, as your program grows, you might imagine it organized by class and superclass, creating a kind of giant tree structure. At the base is the

4. Java Project Design, Input Methods

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

EXCEPTIONS. Prof. Chris Jermaine

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

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

Software Design and Analysis for Engineers

Text Input and Conditionals

Lecture 17: Implementing HashTables 10:00 AM, Mar 5, 2018

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

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Introduction to Computer Science II CS S-22 Exceptions

Proofwriting Checklist

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

Unit Testing as Hypothesis Testing

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

CS Introduction to Data Structures How to Parse Arithmetic Expressions

The Dynamic Typing Interlude

Object Oriented Programming

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

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

CS 3 Introduction to Software Engineering. 3: Exceptions

CS1102: What is a Programming Language?

Menus. You ll find MenuStrip listed in the Toolbox. Drag one to your form. Where it says Type Here, type Weather. Then you ll see this:

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

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

CS1102: Adding Error Checking to Macros

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

Exceptions - Example. Exceptions - Example

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

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

CS159. Nathan Sprague

CS193j, Stanford Handout #25. Exceptions

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Chapter 3.4: Exceptions

Chapter 12 Exception Handling

Object-Oriented Analysis and Design Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology-Kharagpur

Lecture 19 Programming Exceptions CSE11 Fall 2013

Exceptions and Design

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

Chapter 5 Errors. Bjarne Stroustrup

Object-Oriented Principles and Practice / C++

Java Programming Unit 7. Error Handling. Excep8ons.

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

Exception-Handling Overview

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

6.001 Notes: Section 17.5

Post Experiment Interview Questions

Type Checking in COOL (II) Lecture 10

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

Exception Handling Generics. Amit Gupta

CSCI 261 Computer Science II

Exceptions. One-Slide Summary. Language System Structure

Stage 11 Array Practice With. Zip Code Encoding

CMSC 202. Exceptions

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

Transcription:

CS18 Integrated Introduction to Computer Science Fisler, Nelson Lecture 14: Exceptions 10:00 AM, Feb 26, 2018 Contents 1 Exceptions and How They Work 1 1.1 Update to the Banking Example............................. 1 2 Back to the Bank 2 3 What is an Exception? 3 3.1 Creating and Throwing Exceptions............................ 3 3.2 Catching Exceptions.................................... 4 3.3 Understanding Exceptions by Understanding Call Stacks................ 5 3.4 Housekeeping: annotating intermediate methods.................... 6 4 Summarizing Try/Catch blocks 6 4.1 Handling Incorrect Passwords............................... 6 4.2 Optional: Converting One Exception To Another.................... 7 5 Checked Versus Unchecked Exceptions 8 Objectives By the end of these notes, you will know: ˆ when to create and manage your own exceptions ˆ how exceptions are actually processed when programs run By the end of these notes, you will be able to: ˆ create your own exceptions ˆ use try, catch, and throws statements to manage your own exceptions ˆ understand which lines of code do and don t get executed when exceptions are thrown

1 Exceptions and How They Work Recall the banking example from two weeks ago (when we studied about public and private annotations). At the time, we noted that we were just returning strings rather than reporting errors when people provided an incorrect password. By now, you ve worked with exceptions enough to hopefully have guessed that an exception should be thrown here. We haven t studied exceptions formally though. In this lecture, we re going to look carefully at exceptions and how they get evaluated within programs. 1.1 Update to the Banking Example The last time we looked at the banking example, we had the following data organization: ˆ classes for Customers and Accounts ˆ a BankingService containing lists of customers and accounts The version we are starting from today has two main extensions: ˆ We added a BankingConsole class with a simple Scanner-based interface for interacting with the bank ˆ We put the linked lists of customers and accounts inside separate classes, to which we could add methods for searching for items, adding items, and so forth. Creating a separate class like this gives us a way to combine methods specific to our application with built-in Java classes to represent the data. Here is the link to the starter file: http://cs.brown.edu/courses/csci0180/content/sources/exceptions-starter.java 2 Back to the Bank Here s our core question: what should the findbynum and findbyname methods do if no customer or account matched the given input data? Right now, both methods return null, as a way of saying no answer. While this approach gets past the compiler, it is not a good solution because it clutters up the code of other methods that call findbynum and findbyname. Think about getbalance. Ideally, we would like to write this method as follows: class BankingService { public double getbalance(int foracctnum) { Account acct = accounts.findbynum(foracctnum) ; return acct.getbalance(); 2

However, if findbynum returns null (to indicate that no such account number exists), Java will raise an error at runtime when it tries to compute acct.getbalance(). To guard against that, we might modify the code to read: class BankingService { public double getbalance(int foracctnum) { Account acct = accounts.findbynum(foracctnum) ; if (acct!= null) return acct.getbalance(); else return???; // some dummy value must go here This code has two key flaws: 1. The logic of the program has been obscured by the if-statement. The if-statement is really a check on the behavior of a different method (findbynum); it should not clutter up the code of this one. 2. Returning dummy values is never a good idea, because the computation that receives the dummy value must be able to distinguish valid from invalid data. That requires something akin to the if-statement, which we argued against in the previous point. Ideally, we need a way to clearly write methods that use findbynum and findbyname, while letting these two methods alert methods that call them when something goes wrong. The appropriate programming construct for this is called an exception. As the name implies, exceptions are designed to help programs flag and handle situations that would otherwise complicate the normal logic of the program you are trying to write. 3 What is an Exception? Exceptions (or some similar notion) exist in most mainstream programming languages. Intuitively, if a function encounters a situation that is not expected, it does not try to return a normal value. Instead, it announces that a problem occured (by throwing or raising an exception). Other methods watch for announcements and try to recover gracefully. Isn t returning null an announcement that something went wrong? Yes, but we ve just seen that if we return null, the code that called findbynum has to check whether an announcement got made and handle it before calling getbalance. Exceptions use a separate communication channel (as it were) for announcements, This lets methods separate out the normal code from the announcement-handling code; it keeps the code cleaner and will help direct announcements to the part of the code that can best respond to them. We ll make this concept more concrete by working with findbyname. 3

3.1 Creating and Throwing Exceptions Our goal is to replace the return null statement from the current findbyname code with an exception to alert to the rest of the code that something unexpected happened (in this case, the customer was not found). The Java construct that raises alerts is called throw. Our first step, then, is to replace the return null statement with a throw statement: class CustSet implements ICustSet { // return the Customer whose name matches the given name public Customer findbyname(string name) { for (Customer cust:customers) { if (cust.getname() == name) return cust; throw <some object indicating that the name was not found> Next, we need to provide the specific exception to throw. In Java, an exception is an object in the Exception class. We create a subclass of Exception for each different type of alert that we want to raise in our program. In this case, we will create a new exception class for user-not-found errors: class CustNotFoundException extends Exception { String unfoundname; CustNotFoundException(String name) { this.unfoundname = name; An exception subclass should store any information that might be needed later to respond to the exception. In this case, we store the name of the customer that could not be found. This info would be useful, for example, in printing an error message that indicated which specific customer name could not be found. Finally, we modify findbyname to throw a CustNotFoundException if it fails to find the customer. Three modifications are required: ˆ The throw statement needs to be given an object of the CustNotFoundException class to throw. ˆ The findbyname method must declare that it can throw that exception (the compiler needs this information). This occurs in a new throws declaration within the method header, as shown below. ˆ The ICustSet interface, which has the findbyname method header, must also include the throws statement. 4

interface ICustSet { Customer findbyname(string name) throws CustNotFoundException; class CustSet implements ICustSet { // return the Customer whose name matches the given name public Customer findbyname(string name) throws CustNotFoundException { for (Customer cust:customers) { if (cust.getname() == name) return cust; throw new CustNotFoundException(name); 3.2 Catching Exceptions Exceptions are neat because they let us (as programmers) control what part of the code handles the errors that exceptions report. Think about what happens when you encounter a login error when using a modern web-based application: the webpage (your user interface) tells you that your username or password was incorrect and prompts you to try logging in again. That s the same behavior we want to implement here. To do this at the level of code, we will use another new construct in Java called a try-catch block. We try running some method that might result in an exception. If the exception is thrown, we catch it and handle it. Here s a try-catch pair within the loginscreen (which is where we already said we want to handle the error: public void loginscreen() { // prompt for user to enter name and password System.out.println("Welcome to the Bank. Please log in."); System.out.print("Enter your username: "); String username = keyboard.next(); System.out.print("Enter your password: "); int password = keyboard.nextint(); B.login(username,password); System.out.println("Login successful"); catch (CustNotFoundException e) { System.out.println("Login Failed. Try Again"); this.loginscreen(); Notice the try encloses both the call to login and the println that login succeeded. When you set up a try, you have it enclose the entire sequence of statements that should happen if the exception does NOT get thrown. As Java runs your program, if any statement in the try block yields an exception, Java ignores the rest of the try block and hops down to the catch block. Java runs the code in the catch block, and continues from there. 5

Note: if you ve only typed in the code to this point and try to compile, you will get errors regarding the login method hang on we re getting to those by way of the next section.] 3.3 Understanding Exceptions by Understanding Call Stacks To understand how exceptions work, you need to understand a bit more about how Java evaluates your programs. Exceptions aside, what happens under the hood when Java runs your program and someone tries to log in? Our main method started by calling the loginscreen method; this method calls other methods in turn, with methods often waiting on the results of other methods to continue their own computations. Java maintains a stack (we discussed those briefly in the data structures lectures) of method calls that haven t yet completed. When we kick off loginscreen, this stack contains just the call to that method. Separately from the stack, Java starts running the code in your method statement by statement. Imagine an arrow alongside your code that tracks which statement Java is currently evaluating (statements above the arrow are already completed). Switch now to the following PDF, which walks through how Java executes programs with try/catch blocks, showing what we mean by the arrow alongside code. http://cs.brown.edu/courses/csci0180/content/lectures/aux/exceptioncontrolflow.pdf The slideshow you just saw simplifies a couple of details. There may be multiple try markers on the stack (because you can have multiple try blocks), and the stack has ways of remembering where it left off in pending method calls. We ignore those details here in the hopes of giving you the bigger picture. 3.4 Housekeeping: annotating intermediate methods As our demonstration of the stack just showed, the CustNotFoundExn exception passes through certain classes as it comes back from the findbyname method. The Java compiler needs every method to acknowledge what exceptions might get thrown while it is running. We therefore have to add the same throws annotations to each method that does not want to catch the exception as it passes through on the way to the marker. For example, the login method needs to look as follows: public void login(string custname, int withpwd) throws CustNotFoundException { Customer cust = customers.findbyname(custname); cust.trylogin(withpwd); Once you put these additional throws annotations on the code, the code should compile and Java will report failed logins through the loginscreen. 4 Summarizing Try/Catch blocks At this point, you should understand that throw statements go hand-in-hand with try/catch blocks. Whenever a method declares that it can throw an exception, any method that calls it needs a try/catch statement to process the exception. More generally, a try-catch block looks as follows: <the code to run, assuming no exceptions> catch <Exception> { <how to recover from the exception> 6

You can have multiple catch phrases, as we will see in later examples. 4.1 Handling Incorrect Passwords Now that you ve seen one example of exceptions, let s try another. As an exercise for yourself, change the trylogin method in the Customer class (which checks the password) so that it throws an exception from a new class called LoginFailedException. Try it before reading further. You should have ended up with the following: // - - - - - - the new exception class - - - - - - - - class LoginFailedException extends Exception { LoginFailedException(){ // - - - - - - in the customer class - - - - - - - - class Customer { // check whether the given password matches the one for this user public void trylogin(int withpwd) throws LoginFailedException { if (this.password!= withpwd) throw new LoginFailedException(); In addition, we have to add a catch block for these exceptions. Where do we want to catch a FailedLoginException? As before, we would like to catch this in the loginscreen method. We can add another catch block to that method. public void loginscreen() { // prompt for user to enter name and password System.out.println("Welcome to the Bank. Please log in."); System.out.print("Enter your username: "); String username = keyboard.next(); System.out.print("Enter your password: "); int password = keyboard.nextint(); B.login(username,password); System.out.println("Login successful"); catch (CustNotFoundException e) { System.out.println("Login Failed. Try Again"); this.loginscreen(); catch (FailedLoginException e) { System.out.println("Login Failed. Try Again"); this.loginscreen(); As with the CustNotFoundException, you have to put throws annotations on all methods that can either throw or pass along the FailedLoginException. 4.2 Optional: Converting One Exception To Another Some of you may look at the catch clauses in the loginsrcreen method and have some concerns: Both exceptions seem to point to the same core problem (with the same recovery) we couldn t log in the user. Indeed, many systems don t tell the 7

user whether their login problem was due to the username or the password. It s all just one common failed login message and recovery. What if we wanted the loginscreen to only get FailedLoginException, letting the login method somehow convert the CustomerNotFoundException to a FailedLoginException? There s a nice design idea here many methods may call findbyname, but the signicance of a failure to find the user is different in each case. Each such method could do a little processing on the exception to give its callers a more useful interpretation of the problem. Let s do that here: we will have the login method convert the CustomerNotFoundException to a FailedLoginException. This happens within the catch statement. class BankingService { public Customer login(string custname, int withpwd) throws LoginFailedException { Customer cust = customers.findbyname(custname); cust.trylogin(withpwd); return cust; catch (CustNotFoundException e) { throw new LoginFailedException(); Now that this is done, the loginscreen will only ever catch FailedLoginExceptions. // set up a Scanner to read input from the keyboard private Scanner keyboard = new Scanner(System.in); // the method that prompts for input then tries to log in public void loginscreen() { System.out.println("Welcome to the Bank. Please log in."); System.out.print("Enter your username: "); String username = keyboard.next(); System.out.print("Enter your password: "); int password = keyboard.nextint(); this.login(username,password); catch (LoginFailedException e) { System.out.println("Login failed - - please try again\n"); this.loginscreen(); We also must go through ahd fix the throws annotations on each method correspondingly. The final code is available on the notes page. 5 Checked Versus Unchecked Exceptions What we have done so far with try/catch and throw statements are called Checked Exceptions: exceptions that you are using within your application to respond to special situations that arise within your code. With checked exceptions, Java analyzes your code at compile time to make sure that the exceptions will actually be caught (and handled). Sometimes, however, your code fails because of a bug in your code. Null pointer exceptions, array index out of bounds exceptions, and division by zero exceptions are examples of exceptions that alert you to bugs when they are thrown. You don t want to 8

catch these you want to fix your code so that they can t happen again. Put differently, the fix for such situations is to debug your code before you run it. Checked exceptions, in contrast, are for situations you can t control (because they arise from user behavior, for example) that arise while your application is running. These code-error exceptions are special cases of RuntimeExceptions, which we saw earlier in the course. When you throw a runtime exception, you shouldn t catch and manage it (and the compiler won t expect you to). Runtime exceptions are handy when you are working on your code and just want to throw something so you can get past the compiler and test the code you re currently working on. In production code, you only use them for situations in which the program needs to terminate. As a general rule, you should write (and catch) checked exceptions from here on out in homeworks and projects. Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS18 document by filling out the anonymous feedback form: http://cs.brown.edu/courses/cs018/feedback. 9