Administrivia. CPSC Winter 2008 Term 1. Department of Computer Science Undergraduate Events

Similar documents
Administrivia. IBM Info Session Date: Wed,, Jan 13 Time: 5:30 7 pm Location: Wesbrook 100

Location: Planet Laser Interview Skills Workshop

Location: Planet Laser Interview Skills Workshop

public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance;

Location: Planet Laser Interview Skills Workshop

Exceptions - Example. Exceptions - Example

ITI Introduction to Computing II

ITI Introduction to Computing II

More on Exception Handling

Exception-Handling Overview

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

Exceptions. CSC207 Winter 2017

More on Exception Handling

Exceptions Handling Errors using Exceptions

ECE 122. Engineering Problem Solving with Java

1 10 3:30 5: :30 1:30 206, ICICS/CS

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

BBM 102 Introduction to Programming II Spring Exceptions

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

BBM 102 Introduction to Programming II Spring 2017

CS159. Nathan Sprague

CS Programming I: Exceptions

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

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

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

Department of Computer Science Undergraduate Events

CSE 143 Java. Exceptions 1/25/

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

Java. Error, Exception, and Event Handling. Error, exception and event handling. Error and exception handling in Java

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories

Name: Checked: Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab.

Introduction to Software Design

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

EXCEPTIONS. Java Programming

CSE 331 Software Design & Implementation

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

Chapter 13 Exception Handling

Administrivia. Events this week Drop-In Resume and Cover Letter Editing Date: Tues., Mar 23 Time: 12:30 2:30 pm Location: Rm 255, ICICS/CS

EXCEPTION HANDLING. Summer 2018

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

What are Exceptions?

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

Chapter 15 Chapter 11

CS Programming I: Exceptions

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

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. Examples of code which shows the syntax and all that

CS159. Nathan Sprague

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

Input-Output and Exception Handling

2.6 Error, exception and event handling

Contents. I Introductory Examples. Topic 06 -Exception Handling

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Errors and Exceptions

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

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

Fundamentals of Object Oriented Programming

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

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

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

OBJECT ORIENTED PROGRAMMING. Course 6 Loredana STANCIU Room B616

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

Software Practice 1 - Error Handling

Chapter 12 Exception Handling

Name: Checked: Preparation: Investment Calculator with input and output to text files Submit through Blackboard by 8:00am the morning of Lab.

Typecasts and Dynamic Dispatch. Dynamic dispatch

Java Exception. Wang Yang

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

6.Introducing Classes 9. Exceptions

Programming II (CS300)

Exceptions (part 2) An exception is an object that describes an unusual or erroneous situation. Quick Review of Last Lecture.

Exception Handling. Exception Handling

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Programming Languages and Techniques (CIS120)

Why Exceptions? (1.1) Exceptions. Why Exceptions? (1.2) Caller vs. Callee. EECS2030 B: Advanced Object Oriented Programming Fall 2018

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

Exceptions. EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG

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

Software Practice 1 - Error Handling Exception Exception Hierarchy Catching Exception Userdefined Exception Practice#5

Intro to Computer Science II. Exceptions

Full file at Chapter 2 - Inheritance and Exception Handling

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

Administrivia. CSSS Movie Night: Zombieland & Iron Man Date: Thurs., Mar 11 Time: 6 10 pm Location: DMP 310 Free pop & popcorn for every attendee!

Programming for Mobile Computing

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

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

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

Java Loose Ends. 11 December 2017 OSU CSE 1

Internal Classes and Exceptions

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

Introduction to Object-Oriented Programming

CSCI 261 Computer Science II

Exception Handling Generics. Amit Gupta

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

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

Lab 2: File Input and Output

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

Transcription:

Department of Computer Science Undergraduate Events Events this week Drop-In Resume Editing Date: Mon. Jan 11 Time: 11 am 2 pm Location: Rm 255, ICICS/CS Industry Panel Speakers: Managers from IBM, Microsoft, SAP, TELUS, Radical Date: Tues. Jan 12 Time: Panel: 5:15 6:15 pm Networking: 6:15 7:15 pm Location: DMP 110 for panel, X-wing ugrad lounge for networking Tech Career Fair Date: Wed. Jan 13 Time: 10 am 4 pm Location: SUB Ballroom Google Tech Talk Date: Wed, Jan 13 Time: 4 5 pm Location: DMP 110 IBM Info Session Date: Wed,, Jan 13 Time: 5:30 7 pm Location: Wesbrook 100 Administrivia Lecture slides (day by day) are on the web: http://www.cs.ubc.ca/~norm/211/2009w2 Reminder: Assignment #1 is on the web site Due Thursday, January 28 th, 10:00pm How was Lab1? What do you think of the new room?

Where are we? We ve designed a single class Attributes Methods Class invariants (for attributes) Preconditions and postconditions (for methods) Class Design Handling Errors You will be expected to: incorporate exception handling into the design of a method's contract trace code that makes use of exception handling write code to throw, catch or propagate an exception write code that uses a finally block write code to define a new exception class compare and contrast checked and unchecked exceptions understand the consequence of using checked vs. unchecked exceptions Reading: 2 nd Ed: Chapter 15 3 rd, 4 th Ed: Chapter 11 Exercises 2 nd Ed: P15.5, P15.6 (Hint: look at documentation for Scanner class!) 3 rd Ed: P11.9, P11.11 4 th Ed: P11.12, P11.14

Exceptions Why do we need them? Remember the Account class? We added the following precondition to the deposit method: amount >= 0 What if the client fails to check the precondition? The customers won't be happy to find out that sloppy programming has resulted in losing money because of a simple mistake! Class Design - Exceptions 5 Exceptions Why we need them? Rather than using a precondition, we can have the method: return a special value (e.g., true/false) to indicate whether or not the operation was successful problem: print an error message problem: terminate the program problem: Class Design - Exceptions 6

Exceptions Why we need them? Rather than using a precondition or one of the other methods suggested on the previous slide, we can have the method throw an exception if the amount is negative. Benefits: We can force the client to acknowledge the problem. We allow the client to decide how to handle the problem. Class Design - Exceptions 7 What's a Java Exception? An exception is an object with a specific interface, that can be thrown. All exception classes are subclasses of the class Throwable defined in the Java library. Here are some of the methods of this class: Throwable(); Throwable( String message ); String getmessage(); void printstacktrace(); Exceptions encapsulate information about the kind of problem that has occurred (the message) and the sequence of method calls that led to the problem (the stack trace). Class Design - Exceptions 8

What's an exception? There are two types of exception: checked and unchecked. Unchecked exceptions are subclasses of Java s RuntimeException class, while all others are checked exceptions. There is also an Error class that represents abnormal conditions that a program would normally not be expected to handle. Errors are treated like unchecked exceptions. Class Design - Exceptions 9 Java Exception Hierarchy checked exceptions Object Throwable Exception Error IOException RunTimeException unchecked Numerous exceptions and errors are defined in various java packages. i.e., FileNotFoundException in java.io IOException in java.io NullPointerException in java.lang etc. Programmers can define their own exceptions as subclasses of Exception or its subclasses. Class Design - Exceptions 10

Defining an Exception Class Returning to our Account example, suppose we decide to throw an exception when the amount is negative. First we must decide which exception class to use. We could use the class Exception in the Java library but we can capture more information by defining our own exception class. Let's define a class named IllegalValueException to represent the type of exception that will be thrown when we attempt to pass a negative amount. This will be a checked exception (more about this later). Class Design - Exceptions 11 Defining an exception class public class IllegalValueException extends Exception { public IllegalValueException() { public IllegalValueException(String msg) { super(msg); Class Design - Exceptions 12

Throwing an Exception /** * Deposit money into the account * @param amount The amount to be deposited * * @pre true * @post IF amount >= 0 * THEN getbalance() = @pre.getbalance() + amount * ELSE getbalance() = @pre.getbalance() * @throws IllegalValueException if amount is negative */ public void deposit(double amount) throws IllegalValueException { if (amount < 0) throw new IllegalValueException("Error: Neg. amount"); balance = balance + amount; Class Design - Exceptions 13 Handling Exceptions Recall that IllegalValueException is a checked exception. This has consequences for a client calling our deposit method. The client code must do one of the following: catch the exception propagate (i.e., pass on) the exception to its caller (i.e., the method that called it) Class Design - Exceptions 14

Client Catching an Exception public static void main( String[] args ) { Account instructoraccount = new Account ( instructor, 100.0 ); try { instructoraccount.deposit( 100 ); System.out.println( "Balance: " + instructoraccount.getbalance() ); catch( IllegalValueException e ) { System.out.println( e.getmessage() ); What happens when deposit is called? Class Design - Exceptions 15 What happens when this code executes? public static void main( String[] args ) { Account instructoraccount = new Account ( instructor, 100.0 ); try { instructoraccount.deposit( -100 ); System.out.println( "Balance: " + instructoraccount.getbalance() ); catch( IllegalValueException e ) { System.out.println( e.getmessage() ); Class Design - Exceptions 16

Client Propagating an Exception public void deposittoaccount( Account anaccount, double amount ) throws IllegalValueException { anaccount.deposit( amount ); System.out.println( "Balance: " + anaccount.getbalance() ); The method that calls deposit must either: catch the exception propagate the exception to its caller If it propagates the exception then its caller must either catch or propagate the exception and so on Class Design - Exceptions 17