Design by Contract – Exceptions

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Ceg860(Prasad)L10DC1 Design by Contract Invariants Pre-post conditions : Rights and Obligations Exceptions : Contract Violations.
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Chair of Software Engineering OOSC - Lecture 23 1 Object-Oriented Software Construction Bertrand Meyer.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 6: Exception Handling.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 15: Exception handling.
-5- Exception handling What is an exception? “An abnormal event” Not a very precise definition Informally: something that you don’t want to happen.
Exceptions David Rabinowitz. March 3rd, 2004 Object Oriented Design Course 2 The Role of Exceptions Definition: a method succeeds if it terminates in.
Ranga Rodrigo. Class is central to object oriented programming.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
PRAGMATIC PARANOIA Steven Hadfield & Anthony Rice.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Exceptions and assertions CSE 331 University of Washington.
Design by Contract Ranga Rodrigo based on Mark Priestley's Lectures.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 1 COSC3311 – Software Design Loop invariant/variant.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
1 Exceptions When the Contract is Broken. 2 Definitions A routine call succeeds if it terminates its execution in a state satisfying its contract A routine.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
1 ָ נן oop uException-Handling Mechanism – similar to C++ l throw e signals the occurrence of an exception lThe statement try/catch allows the calling.
Eighth Lecture Exception Handling in Java
Exceptions handling Try, catch blocks Throwing exceptions.
Chapter 16 Exception Handling
similar concepts, different syntax
Exceptions In this lecture:
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Chapter 10 – Exception Handling
Handling Exceptionally Sticky Problems
MIT AITI 2003 Lecture14 Exceptions
CS1101: Programming Methodology Recitation 7 – Exceptions
Exception Handling and Event Handling
User-Defined Functions
EE422C Software Implementation II
Software Construction
Exceptions & exception handling
Phil Tayco Slide version 1.0 Created Nov. 26, 2017
CMSC 202 Exceptions.
Exceptions & exception handling
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Fundamental Error Handling
Part B – Structured Exception Handling
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Exceptions handling Try, catch blocks Throwing exceptions.
CMSC 202 Exceptions 2nd Lecture.
CSC 143 Java Errors and Exceptions.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Handling Exceptionally Sticky Problems
Java Basics Exception Handling.
Exceptions for safe programming.
CMSC 202 Exceptions.
Exception Handling.
Software Construction
Exceptions and Exception Handling
Design Contracts and Errors A Software Development Strategy
Presentation transcript:

Design by Contract – Exceptions 9/10/2019 8:50 AM COSC3311 – Software Design Design by Contract – Exceptions Object Oriented Software Construction 10/09/2019 8:50 AM 0

OOSC2 Ch. 12 Exceptions Correct use of exceptions Division by zero Run out of memory when creating an object A void call A contract violation (See OOSC2 page 413) Correct use of exceptions Technique of last resort! Do not use it for normal flow of control Object Oriented Software Construction 10/09/2019 8:50 AM 1

Rescue Clause count: INTEGER transmit (m: MESSAGE) -- transmit ‘m’ on redundant channels 1..count local i: INTEGER do sent_to_transmitter(m,i) rescue i := i + 1 if i <= count then retry end Object Oriented Software Construction 10/09/2019 8:50 AM 2

The original strategy r (...) is require ... do op1 op2 opi opn rescue end Fails, triggering an exception in r (r is recipient of exception). Object Oriented Software Construction 10/09/2019 8:50 AM 3

The call chain Routine call r0 r1 r2 r3 r4 Object Oriented Software Construction 10/09/2019 8:50 AM 4

Handling exceptions properly Safe exception handling principle: There are only two acceptable ways to react for the recipient of an exception: Concede failure, and trigger an exception in the caller (Organized Panic). Try again, using a different strategy (or repeating the same strategy) (Retrying). Object Oriented Software Construction 10/09/2019 8:50 AM 5

9/10/2019 8:50 AM How not to do it Normal Return to Caller as if nothing strange has happened (might cause an artillery shot to head in the wrong direction) (From an Ada textbook) sqrt (x: REAL) return REAL is begin if x < 0.0 then raise Negative; else normal_square_root_computation; end exception when Negative => put ("Negative argument"); return; when others =>  Ada does not have assertions – hence defensive for negative The main problem is that put(“Negative argument”) return will return to the original caller as if nothing strange has happened (this might direct the artillery shot in the wrong direction). Object Oriented Software Construction 10/09/2019 8:50 AM 6

Java Version – also problematic public class Sample {     public static void main (String[] args) {         System.out.println(sqrt(523)); //23 x 23     }     private static double sqrt (double i) {         double result = -1;         try{             result = Math.sqrt(i);         } catch (ArithmeticException ae){              System.out.println("Real numbers only!"); // i.e. do a normal return // as if nothing has happened!                    }         return result;      } } Object Oriented Software Construction 10/09/2019 8:50 AM 7

Better Java Code Assume we create a subclass of java.lang.Exception called SquareRootException.   The sqrt method should be changed as follows: private static double sqrt (double x) throws SquareRootException {          double result = -1;          try {result = Math.sqrt(x)} catch (ArithmeticException ae) {              throw new SquareRootException ("Bad param:"+x);             }          return result;        } Object Oriented Software Construction 10/09/2019 8:50 AM 8

Dealing with Exceptions in Java 9/10/2019 8:50 AM Dealing with Exceptions in Java We as programmers want to write quality code that solves problems. Unfortunately, exceptions come as side effects of our code. No one likes side effects, so we soon find our own ways to get around them. I have seen some smart programmers deal with exceptions the following way: public void consumeAndForgetAllExceptions () { try { ...some code that throws exceptions } catch (Exception ex){ ex.printStacktrace(); } } What is wrong with the code above? Once an exception is thrown, normal program execution is suspended and control is transferred to the catch block. The catch block catches the exception and just suppresses it. Execution of the program continues after the catch block, as if nothing had happened. http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html Object Oriented Software Construction 10/09/2019 8:50 AM 9

Better public void dontconsumeAndForgetAllExceptions () { try { ...some code that throws exceptions } catch (Exception ex) { throw new RuntimeException(ex); } Object Oriented Software Construction 10/09/2019 8:50 AM 10

Eiffel Exception mechanism Two constructs: A routine may contain a rescue clause. A rescue clause may contain a retry instruction. A rescue clause that does not execute a retry leads to failure of the routine (this is the organized panic case). Object Oriented Software Construction 10/09/2019 8:50 AM 11

Transmitting over an unreliable line (1) Max_attempts: INTEGER is 100 attempt_transmission (message: STRING) is -- Transmit message in at most -- Max_attempts attempts. local failures: INTEGER do unsafe_transmit (message) rescue failures := failures + 1 if failures < Max_attempts then retry end end Object Oriented Software Construction 10/09/2019 8:50 AM 12

Transmitting over an unreliable line (2) Max_attempts: INTEGER = 100 failed: BOOLEAN attempt_transmission (message: STRING) -- Try to transmit message; -- if impossible in at most Max_attempts -- attempts, set failed to true. local failures: INTEGER do if failures < Max_attempts then unsafe_transmit (message) else failed := True end rescue failures := failures + 1 retry Object Oriented Software Construction 10/09/2019 8:50 AM 13

If no exception clause (1) Absence of a rescue clause is equivalent, in first approximation, to an empty rescue clause: f (...) is do ... end is an abbreviation for f (...) is do ... rescue -- Nothing here end (This is a provisional rule; see next.) Object Oriented Software Construction 10/09/2019 8:50 AM 14

The correctness of a class create a.make (…) (1-n) For every exported routine r: {INV and prer} dor {INV and postr} (1-m) For every creation procedure cp: {precp} docp {postcp and INV} S1 a.f (…) S2 a.g (…) S3 a.f (…) S4 Object Oriented Software Construction 10/09/2019 8:50 AM 15

Exception correctness: A quiz For the normal body: {INV and prer} dor {INV and postr} For the exception clause: { ??? } rescuer { ??? } Object Oriented Software Construction 10/09/2019 8:50 AM 16

Quiz answers For the normal body: {INV and prer} dor {INV and postr} For the exception clause: {True} rescuer {INV} Object Oriented Software Construction 10/09/2019 8:50 AM 17

If no exception clause (2) Absence of a rescue clause is equivalent to a default rescue clause: f (...) is do ... end is an abbreviation for f (...) is do ... rescue default_rescue end The task of default_rescue is to restore the invariant. Object Oriented Software Construction 10/09/2019 8:50 AM 18

The Cook and the Firefighter (1) Two noble professions Cook Firefighter Object Oriented Software Construction 10/09/2019 8:50 AM 19

The Cook and the Firefighter (2) assumes when showing up for work that the restaurant is not on fire (invariant) must prepare a good meal (postcondition) Firefighter May assume nothing about the state of the restaurant on first entry (True precondition) Returns the restaurant to the non-burning state (invariant) Not responsible for cooking a meal. Object Oriented Software Construction 10/09/2019 8:50 AM 20

For finer-grain exception handling Use class EXCEPTIONS from the Kernel Library. Some features: exception (code of last exception that was triggered). is_assertion_violation, etc. raise (“exception_name”) Object Oriented Software Construction 10/09/2019 8:50 AM 21