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

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

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

CS61B Lecture #12. Programming Contest: Coming up Saturday 5 October. See the contest announcement page, here.

CS61B Lecture #12. Public Service Announcement. Miscellaneous Topics: What to do About Errors? throw new SomeException (optional description);

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

CS159. Nathan Sprague

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

CS61B Lecture #13: Packages, Access, Etc.

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

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Exceptions. CSC207 Winter 2017

CSE 331 Software Design & Implementation

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

CS61B Lecture #13: Packages, Access, Etc.

Exceptions Handling Errors using Exceptions

CS 251 Intermediate Programming Exceptions

Exceptions and assertions

Exceptions in Java

Programming Languages and Techniques (CIS120)

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

ITI Introduction to Computing II

Software Construction

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

ITI Introduction to Computing II

Programming II (CS300)

2IP15 Programming Methods

Programming II (CS300)

Designing Robust Classes

CS112 Lecture: Exceptions and Assertions

17. Handling Runtime Problems

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

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

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

Chapter 13 Exception Handling

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

Exception-Handling Overview

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

Assertions and Exceptions Lecture 11 Fall 2005

CS159. Nathan Sprague

CSE 331 Software Design and Implementation. Lecture 12 Assertions & Exceptions

CSE 331 Software Design and Implementation. Lecture 12 Assertions & Exceptions

2.6 Error, exception and event handling

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

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Data Structures. 02 Exception Handling

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

16-Dec-10. Consider the following method:

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

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

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

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

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

Errors and Exceptions

Computer Science is...

Internal Classes and Exceptions

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

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

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

CS 3 Introduction to Software Engineering. 3: Exceptions

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

Exceptions and Error Handling

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

Lecture 19 Programming Exceptions CSE11 Fall 2013

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

Lecture 10 Assertions & Exceptions

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

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

Java Loose Ends. 11 December 2017 OSU CSE 1

Java Object Oriented Design. CSC207 Fall 2014

CS11 Java. Fall Lecture 4

COMP1008 Exceptions. Runtime Error

Writing your own Exceptions. How to extend Exception

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

Lecture 12 Assertions & Exceptions

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

CSCI 261 Computer Science II

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

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

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

ECE 122. Engineering Problem Solving with Java

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

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

COE318 Lecture Notes Week 10 (Nov 7, 2011)

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

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Comp 249 Programming Methodology Chapter 9 Exception Handling

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

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

JAVA BASICS II. Example: FIFO

BBM 102 Introduction to Programming II Spring Exceptions

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

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

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

CSE 307: Principles of Programming Languages

Programming Languages and Techniques (CIS120e)

CSE 143 Java. Exceptions 1/25/

Exceptions and Design

Programming Languages and Techniques (CIS120)

Transcription:

CS61B Lecture #12 Today: Various odds and ends in support of abstraction. Readings: At this point, we have looked at Chapters 1 9 of Head First Java. Today s lecture is about Chapters 9 and 11. For Friday, please read Chapter 10 of HFJ and Chapter 3 ( Numbers ) from Assorted Material on Java. Last modified: Mon Oct 22 15:34:06 2007 CS61B: Lecture #12 1

Parent constructors In lecture notes #5, talked about how Java allows implementer of a class to control all manipulation of objects of that class. In particular, this means that Java gives the constructor of a class the first shot at each new object. When one class extends another, there are two constructors one for the parent type and one for the new (child) type. In this case, Java guarantees that one of the parent s constructors is called first. In effect, there is a call to a parent constructor at the beginning of every one of the child s constructors. You can call the parent s constructor yourself. By default, Java calls the default (parameterless) constructor. class Figure { class Rectangle extends Figure { public Figure (int sides) { public Rectangle () {... super (4); }... }... } } Last modified: Mon Oct 22 15:34:06 2007 CS61B: Lecture #12 2

What to do About Errors? Large amount of any production program devoted to detecting and responding to errors. Some errors are external (bad input, network failures); others are internal errors in programs. When method has stated precondition, it s the client s job to comply. Still, it s nice to detect and report client s errors. In Java, we throw exception objects, typically: throw new SomeException (optional description); Exceptions are objects. By convention, they are given two constructors: one with no arguments, and one with a descriptive string argument (which the exception stores). Java system throws some exceptions implicitly, as when you dereference a null pointer, or exceed an array bound. Last modified: Mon Oct 22 15:34:06 2007 CS61B: Lecture #12 3

Catching Exceptions A throw causes each active method call to terminate abruptly, until (and unless) we come to a try block. Catch exceptions and do something corrective with try: try { Stuff that might throw exception; } catch (SomeException e) { Do something reasonable; } catch (SomeOtherException e) { Do something else reasonable; } Go on with life; When SomeException exception occurs in Stuff..., we immediately do something reasonable and then go on with life. Descriptive string (if any) available as e.getmessage() for error messages and the like. Last modified: Mon Oct 22 15:34:06 2007 CS61B: Lecture #12 4

Exceptions: Checked vs. Unchecked TheobjectthrownbythrowcommandmustbeasubtypeofThrowable (in java.lang). Java pre-declares several such subtypes, among them Error, used for serious, unrecoverable errors; Exception, intended for all other exceptions; RuntimeException, a subtype of Exception intended mostly for programming errors too common to be worth declaring. Pre-declared exceptions are all subtypes of one of these. Any subtype of Error or RuntimeException is said to be unchecked. All other exception types are checked. Last modified: Mon Oct 22 15:34:06 2007 CS61B: Lecture #12 5

Unchecked Exceptions Intended for Programmer errors: many library functions throw IllegalArgumentException when one fails to meet a precondition. Errors detected by the basic Java system: e.g., Executing x.y when x is null, Executing A[i] when i is out of bounds, Executing(String) xwhenxturnsoutnottopointtoastring. Certain catastrophic failures, such as running out of memory. May be thrown anywhere at any time with no special preparation. Last modified: Mon Oct 22 15:34:06 2007 CS61B: Lecture #12 6

Checked Exceptions Intended to indicate exceptional circumstances that are not necessarily programmer errors. Examples: Attempting to open a file that does not exist. Input or output errors on a file. Receiving an interrupt. Every checked exception that can occur inside a method must either be handled by a try statement, or reported in the method s declaration. For example, void myread () throws IOException, InterruptedException {... } means that myread (or something it calls) might throw IOException or InterruptedException. Language Design: Why did Java make the following illegal? class Parent { class Child extends Parent { void f () {... } void f () throws IOException {... } } } Last modified: Mon Oct 22 15:34:06 2007 CS61B: Lecture #12 7

Good Practice Throw exceptions rather than using print statements and System.exit everywhere,...because response to a problem may depend on the caller, not just method where problem arises. Nice to throw an exception when programmer violates preconditions. Particularly good idea to throw an exception rather than let bad input corrupt a data structure. Good idea to document when methods throw exceptions. To convey information about the cause of exceptional condition, put it into the exception rather than into some global variable: class MyBad extends Exception { try {... public IntList errs; } catch (MyBad e) { MyBad (IntList nums) { errs=nums; }... e.errs... } } Last modified: Mon Oct 22 15:34:06 2007 CS61B: Lecture #12 8