Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

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

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

Assertions, pre/postconditions

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

CSC Java Programming, Fall Java Data Types and Control Constructs

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

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

An exception is simply an error. Instead of saying an error occurred, we say that an.

Program Correctness and Efficiency. Chapter 2

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

CS 3 Introduction to Software Engineering. 3: Exceptions

CS112 Lecture: Exceptions and Assertions

Assertions and Exceptions Lecture 11 Fall 2005

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

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

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Exceptions. CSC207 Winter 2017

EECS1710. Checklist from last lecture (Sept 9, 2014) " get an EECS account (if you don t have it already) " read sections

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

Object Oriented Programming

Designing Robust Classes

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

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

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

Object Oriented Programming Exception Handling

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

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

What we are reinforcing with the exercises this class

Classes, interfaces, & documentation. Review of basic building blocks

Programming II (CS300)

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Pace University. Fundamental Concepts of CS121 1

JPred-P 2. Josh Choi, Michael Welch {joshchoi,

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

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

EECS 1001 and EECS 1030M, lab 01 conflict

Programming II (CS300)

Exceptions in Java

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

Object-Oriented Programming

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

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

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

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.


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

Comparing procedure specifications

CPSC 427: Object-Oriented Programming

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

Object Oriented Programming

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

Class Meeting 05 (Lecture 04) Objectives for this class meeting. Conduct vote on basic style of game for class project

Structure of Programming Languages Lecture 10

Software Design and Analysis for Engineers

Chapter 4 Defining Classes I

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

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

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

Exceptions and assertions

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

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

Assertions. Assertions - Example

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

COSC Exception Handling. Yves Lespérance. Lecture Notes Week 10 Exception Handling

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

CSE1710. Big Picture. Today is last day covering Ch 6. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

COE318 Lecture Notes Week 10 (Nov 7, 2011)

CS 161 Computer Security

Internal Classes and Exceptions

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

CSE 143: Computer Programming II Summer 2017 HW5: Anagrams (due Thursday, August 3, :30pm)

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

ITI Introduction to Computing II

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

Advanced Flow of Control -- Introduction

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

Testing Exceptions with Enforcer

Exception Handling Generics. Amit Gupta

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

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

ITI Introduction to Computing II

ECE 122. Engineering Problem Solving with Java

Exception-Handling Overview

CSE 331 Software Design & Implementation

More on Objects in JAVA TM

An Overview of the BLITZ System

Readability [Skrien 4.0] Programs must be written for people to read, and only incidentally for machines to execute.

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Checklist (for Today) What we are reinforcing with the exercises this class. recognizing the various different arithmetic operators (in situ)

JAVA BASICS II. Example: FIFO

CIS192 Python Programming

Chapter 11 Exception Handling

Java: advanced object-oriented features

Java Loose Ends. 11 December 2017 OSU CSE 1

Lecture Notes on Memory Layout

Data Structure. Recitation IV

a correct statement? You need to know what the statement is supposed to do.

Transcription:

CSE1720 Click to edit Master Week text 01, styles Lecture 02 Second level Third level Fourth level Fifth level Winter 2015! Thursday, Jan 8, 2015 1 Objectives for this class meeting 1. Conduct review of core concepts concerning contracts and pre/post conditions 2. Complete and discuss questions about Exceptions 2 1

Recap about the Utility and Non-Utility Classes 3 utility classes:! cannot be instantiated; no constructor (not a public one, at least)! all methods and/or fields are static! e.g., the class Toolbox non-utility classes:! can be instantiated, has public constructor! may include both non-static and static methods and/or fields! e.g., the class Integer! Integer.MAX_INT is a static field and Integer.parseInt is a static method.! tostring() is a non-static method (The class has no non-static fields) Recap about the Client View! implementers: offers services in the form of classes (utility and non-utility classes)! clients: make use of the services offered by implementers, subject to the Pre and Post conditions 4 2

5 Recap about the PRE and POST Conditions! PRE condition(s) that the client must satisfy! POST condition(s) that the implementer must satisfy! PRE and POST conditions are used to establish correctness.! Does the service function according to their specification?! This is done during development, before services are released and go into production! PRE and POST conditions eliminate redundancy.! Is the CLIENT or IMPLEMENTER responsible for checking the value of the parameters being passed to methods?! In the absence of information, both might do this. This is redundant and inefficient. PRE and POST: the Client View! the Pre and Post conditions can be specified in the API or in other documentation! the style and notation may vary! the Pre and Post conditions may formulated in terms of:! a boolean expression that must evaluate to true! a written statement that must be followed! the next two slides are examples that show both notations, sideby-side 6 3

PRE Example #1 suppose we have the method public String fraxas(int num) PRECONDITION (written version): num must be strictly greater than 0! for the PRE to be met, the written description must hold PRECONDITION (boolean expression version): num > 0! for the PRE to be met, num must be such that num > 0 == true 7 PRE Example #2 see sec 2.3.3 for further review suppose we have the method public String saxa(int num) PRECONDITION (written version): num can be any number! for the PRE to be met, the written description must hold PRECONDITION (boolean expression version): true! for the PRE to be met, num must be s.t. true == true (or just true) but it will always be true (thanks to strong typing) but any value of num will satisfy this, since the boolean expression does not even depend on num 8 4

More about the true PRE! a pre condition of true means that satisfaction of the precondition does not have dependency on the client's parameter values! Thus, the client can provide any sort of parameter values and should be assured that the service should behave as specified.! If the service does not behave as specified " FAULT OF SERVICE! Otherwise, if the PRE is not the boolean literal true, then the client has some sort of burden in satisfying the precondition.! If client fails to do so! and if the service does not behave as specified " FAULT OF CLIENT 9 PRE and POST: the Client View! in Java, the PRE is found under "Parameters"! most often the PRE is true! such preconditions are simply omitted from the API! this means the client needs simply to provide the parameter values (and nothing further in terms of conditions on those values) 10! in Java, the POST is found under "Returns" and "Throws"! it quite often happens that the POST consists of both: 1. a specification of the return, and 2. a specification of the condition under which exceptions are thrown 10 5

Example no precondition is specified PRE is true 11 returns and throws are parts of the post condition 11 Multiple Choice Quiz! complete the questions to the best of your abilities! you can discuss with your neighbours! we will discuss the answers, pending time 12 12 6

Q1 - Example int denom = 0; int result = 7 / denom; The compiler finds the two statements above to obey all syntax and semantic rules, yet the resulting bytecode contains invalid instructions. 13 13 Q3 Exceptions may be thrown due to invalid instructions. Exceptions may be thrown due to bytecode instruction!! For example: the method parseint in Integer may through an exception. The implementer of the class Integer accomplished this by design (by using a statement that makes use of the reserved word throw) 14 14 7

Q4 - Example! Suppose the specification of App01 states that the app prompts the user for two numbers and will output the product. The specification also states the user does not enter two numbers, then the app prints a friendly message and terminates.! When we run App01, it never crashes. However, when the user enters two numbers, it always outputs the sum of the two numbers.! The bytecode for App01 does not contain any invalid instructions, and yet it is not correct. 15 Q5! The running environment consists of the VM, the operating system, the hardware, and the network, if any. (p.429) 16 8

Q7 counter example! Here is an app that throws an exception, yet never crashes: 17 Q8! This question doesn t make any sense!!!! The definition of a crash is that the VM terminates the program with a non-clean exit.! A non-clean exit, by definition, is an exit without the VM conducting an orderly shutdown 18 9

Q9! The VM certainly does have a shutdown routine!!! see the slides from L01 19 Q10! the API for a service may stipulate that an exception may be thrown under certain conditions.! The potential for an exception is part of the post-condition.! If an exception is thrown, it is not punishment. The method may be merely following its contract.! if the client violates the pre-condition, the punishment is that the implementer is no longer obligated to follow their post condition! this might even mean that an exception is NOT thrown, when the client may be expecting this! 20 10

Q11! exceptions are not intrinsically bad they are often a features of services! if an app does not handle exceptions that may potentially arise, this is usually a sign of a not-so-great implementation 21 Q12! The API should specify the PRE and POST. It should not specify consequences.! Knowing the consequences for pre- and post-condition violation is part of the knowledge base that software developers should have. It is not included in the API documentation. 22 11

Q13! if a service potentially throws an exception, this will be documented in the post condition.! It doesn t make any sense for it to be elsewhere in the API! It violates the principle of contracts if the API omits this piece of information. 23 Q14! The delegation model is built into the VM, it doesn t change depending on the app that the VM is asked to invoke. 24 12

Q15! If class A uses the services of class B, then A is the client of B!! The is-a-client-of relationship is transitive! E.g., A is a client of B, B is a client of C, C is a client of D By transitivity, A is an indirect client of D The chain of client relationships, during method invocations, defines the invocation trail. The trail starts with the main method and ends with the client that does not delegate to any other method. 25 Q16! We start searching for a handler in the currently executing method! the search is transferred to the caller method! repeat up the trail until we reach the main method (all calls ultimately originate in the main method)! the trail of the search can be seen in the stack trace! 26 13

Q17! searching down the invocation trail would mean starting in the main method, and then going down the stack trace! This might be technically possible to do, but wouldn t be a great idea! it would violate the principle of delegation 27 Q18! the designers of Java actually have the converse of this policy, they don t want software developers to explicitly handle or delegate all exceptions! we know this because some exceptions are checked and others are unchecked (sec 11.3.3)! if a client makes use of a services that potentially throws a checked exception, the compiler will enforce exception handling! this means that the client must explicitly handle the exception! either with a try-catch or a throws clause! if no explicit handling, compiler error!!! but a big subset of the exceptions are unchecked!! 28 14