Advanced Topics in Object Technology
-
Upload
calista-schneider -
Category
Documents
-
view
27 -
download
0
description
Transcript of Advanced Topics in Object Technology
![Page 1: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/1.jpg)
ATOT - Lecture 25, 30 June 2003
1
Chair of Software Engineering
Advanced Topics in Object Technology
Bertrand Meyer
![Page 2: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/2.jpg)
ATOT - Lecture 25, 30 June 2003
2
Chair of Software Engineering
Lecture 25: Exception handling
by Karine Arnout
![Page 3: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/3.jpg)
ATOT - Lecture 25, 30 June 2003
3
Chair of Software Engineering
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.
![Page 4: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/4.jpg)
ATOT - Lecture 25, 30 June 2003
4
Chair of Software Engineering
The original strategy
r (...) isrequire
...do
op1
op2
...opi
...opn
ensure...
end
Fails, triggering an exception in r (r is recipient of exception).
![Page 5: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/5.jpg)
ATOT - Lecture 25, 30 June 2003
5
Chair of Software Engineering
Causes of exceptions
Assertion violation Void call (x.f with no object attached to x) Operating system signal (arithmetic overflow, no
more memory, interrupt ...)
![Page 6: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/6.jpg)
ATOT - Lecture 25, 30 June 2003
6
Chair of Software Engineering
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).
![Page 7: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/7.jpg)
ATOT - Lecture 25, 30 June 2003
7
Chair of Software Engineering
(From an Ada textbook)
sqrt (x: REAL) return REAL isbegin
if x < 0.0 thenraise Negative;
elsenormal_square_root_computation;
endexception
when Negative =>put ("Negative argument");return;
when others => end; -- sqrt
How not to do it
![Page 8: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/8.jpg)
ATOT - Lecture 25, 30 June 2003
8
Chair of Software Engineering
The call chain
r0
r1
r2
r3
r4
Routine call
![Page 9: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/9.jpg)
ATOT - Lecture 25, 30 June 2003
9
Chair of Software Engineering
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).
![Page 10: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/10.jpg)
ATOT - Lecture 25, 30 June 2003
10
Chair of Software Engineering
Transmitting over an unreliable line (1)
Max_attempts: INTEGER is 100
attempt_transmission (message: STRING) is-- Transmit message in at most -- Max_attempts attempts.
localfailures: INTEGER
dounsafe_transmit (message)
rescuefailures := failures + 1if failures < Max_attempts then
retryend
end
![Page 11: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/11.jpg)
ATOT - Lecture 25, 30 June 2003
11
Chair of Software Engineering
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.
localfailures: INTEGER
doif failures < Max_attempts then
unsafe_transmit (message)else
failed := Trueend
rescuefailures := failures + 1retry
end
![Page 12: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/12.jpg)
ATOT - Lecture 25, 30 June 2003
12
Chair of Software Engineering
If no exception clause (1)
Absence of a rescue clause is equivalent, in first approximation, to an empty rescue clause:
f (...) isdo
...end
is an abbreviation for
f (...) isdo
...rescue
-- Nothing hereend
(This is a provisional rule; see next.)
![Page 13: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/13.jpg)
ATOT - Lecture 25, 30 June 2003
13
Chair of Software Engineering
The correctness of a class
(1-n) For every exported routine r:
{INV and Prer} dor {Postr and INV}
(1-m) For every creation procedure cp:
{Precp} docp {Postcp and INV}
a.f (…)
a.g (…)
a.f (…)
create a.make (…)S1
S2
S3
S4
![Page 14: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/14.jpg)
ATOT - Lecture 25, 30 June 2003
14
Chair of Software Engineering
Exception correctness: A quiz
For the normal body:
{INV and Prer} dor {Postr and INV}
For the exception clause:
{ ??? } rescuer { ??? }
![Page 15: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/15.jpg)
ATOT - Lecture 25, 30 June 2003
15
Chair of Software Engineering
Quiz answers
For the normal body:
{INV and Prer} dor {Postr and INV}
For the exception clause:
{True} rescuer {INV}
![Page 16: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/16.jpg)
ATOT - Lecture 25, 30 June 2003
16
Chair of Software Engineering
If no exception clause (2)
Absence of a rescue clause is equivalent to a default rescue clause:
f (...) isdo
...end
is an abbreviation for
f (...) isdo
...rescue
default_rescueend
The task of default_rescue is to restore the invariant.
![Page 17: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/17.jpg)
ATOT - Lecture 25, 30 June 2003
17
Chair of Software Engineering
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”)
![Page 18: Advanced Topics in Object Technology](https://reader036.fdocuments.us/reader036/viewer/2022082818/5681320d550346895d98603a/html5/thumbnails/18.jpg)
ATOT - Lecture 25, 30 June 2003
18
Chair of Software Engineering
End of lecture 25