Chair of Software Engineering ATOT - Lecture 25, 30 June 2003 1 Advanced Topics in Object Technology Bertrand Meyer.

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

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Ceg860(Prasad)L10DC1 Design by Contract Invariants Pre-post conditions : Rights and Obligations Exceptions : Contract Violations.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 10: Advanced Object-Oriented Mechanisms (based on.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering OOSC - Lecture 23 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
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.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 10, 5 May 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.
Chair of Software Engineering ATOT - Lecture 24, 25 June Advanced Topics in Object Technology Bertrand Meyer.
1 Advanced Material The following slides contain advanced material and are optional.
Exceptions (Large parts of these copied from Ed Schonberg’s slides)
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Chair of Software Engineering ATOT - Lecture 26, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
Chair of Software Engineering ATOT - Lecture 8, 28 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Chair of Software Engineering 1 Introduction to Programming Exercise Session Week 9 M. Piccioni 17/18 November 2008.
Chair of Software Engineering ATOT - Lecture 20, 11 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
Chair of Software Engineering ATOT - Lecture 14, 19 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 7: Inheritance.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
1 © Wolfgang Pelz Design by Contract Design by Contract™ Based on material drawn from: Bertrand.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
Ranga Rodrigo. Class is central to object oriented programming.
PRAGMATIC PARANOIA Steven Hadfield & Anthony Rice.
PZ11A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ11A - Exception handling Programming Language Design.
Chapter 13, Slide 1 Exception Handling Exception handling is a language feature that allows the programmer to handle runtime "exceptional conditions."
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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Design by Contract Ranga Rodrigo based on Mark Priestley's Lectures.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 1 COSC3311 – Software Design Loop invariant/variant.
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.
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.
SENG521 (Fall SENG 521 Software Reliability & Testing Fault Tolerant Software Systems: Techniques (Part 4a) Department of Electrical.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
1 Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
© 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
PZ11A Programming Language design and Implementation -4th Edition
similar concepts, different syntax
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Control Hazards Branches (conditional, unconditional, call-return)
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
8 Once features.
Once features.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Design by Contract – Exceptions
Presentation transcript:

Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer

Chair of Software Engineering ATOT - Lecture 25, 30 June Lecture 25: Exception handling by Karine Arnout

Chair of Software Engineering ATOT - Lecture 25, 30 June Exception handling  The need for exceptions arises when the contract is broken.  Two concepts:  Failure: a routine, or other operation, is unable to fulfill its contract.  Exception: an undesirable event occurs during the execution of a routine — as a result of the failure of some operation called by the routine.

Chair of Software Engineering ATOT - Lecture 25, 30 June The original strategy r (...) is require... do op 1 op 2... op i... op n ensure... end Fails, triggering an exception in r (r is recipient of exception).

Chair of Software Engineering ATOT - Lecture 25, 30 June Causes of exceptions  Assertion violation  Void call (x.f with no object attached to x)  Operating system signal (arithmetic overflow, no more memory, interrupt...)

Chair of Software Engineering ATOT - Lecture 25, 30 June 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).

Chair of Software Engineering ATOT - Lecture 25, 30 June (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 =>  end; -- sqrt How not to do it

Chair of Software Engineering ATOT - Lecture 25, 30 June The call chain r0r0 r1r1 r2r2 r3r3 r4r4 Routine call

Chair of Software Engineering ATOT - Lecture 25, 30 June 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).

Chair of Software Engineering ATOT - Lecture 25, 30 June 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 retryend

Chair of Software Engineering ATOT - Lecture 25, 30 June Transmitting over an unreliable line (2) Max_attempts: INTEGER is 100 failed: BOOLEAN attempt_transmission (message: STRING) is -- 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 end

Chair of Software Engineering ATOT - Lecture 25, 30 June 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.)

Chair of Software Engineering ATOT - Lecture 25, 30 June The correctness of a class  (1-n) For every exported routine r: {INV and Pre r } do r {Post r and INV}  (1-m) For every creation procedure cp: {Pre cp } do cp {Post cp and INV} a.f (…) a.g (…) a.f (…) create a.make (…) S1 S2 S3 S4

Chair of Software Engineering ATOT - Lecture 25, 30 June Exception correctness: A quiz  For the normal body: {INV and Pre r } do r {Post r and INV}  For the exception clause: { ??? } rescue r { ??? }

Chair of Software Engineering ATOT - Lecture 25, 30 June Quiz answers  For the normal body: {INV and Pre r } do r {Post r and INV}  For the exception clause: {True} rescue r {INV}

Chair of Software Engineering ATOT - Lecture 25, 30 June 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.

Chair of Software Engineering ATOT - Lecture 25, 30 June For finer-grain exception handling  Use class EXCEPTIONS from the Kernel Library.  Some features:  exception (code of last exception that was triggered).  assertion_violation, etc.  raise (“exception_name”)

Chair of Software Engineering ATOT - Lecture 25, 30 June End of lecture 25