• Home
  • Textbooks
  • Real-Time Systems and Programming Languages: Ada, Real-Time Java and C/Real-Time POSIX
  • Exceptions and exception handling

Real-Time Systems and Programming Languages: Ada, Real-Time Java and C/Real-Time POSIX

Alan Burns, Andy Wellings

Chapter 3

Exceptions and exception handling - all with Video Answers

Educators


Chapter Questions

Problem 1

Compare and contrast the exception-handling and recovery block approaches to software fault tolerance.

Check back soon!

Problem 2

Give examples of:
(1) a synchronous exception detected by the application
(2) an asynchronous exception detected by the application
(3) a synchronous exception detected by the execution environment
(4) an asynchronous exception detected by the execution environment

Check back soon!

Problem 3

The package Character_Io whose specification is given below, provides a function for reading characters from the terminal. It also provides a procedure for throwing away all the remaining characters on the current line. The package may raise the exception Io_Error.
package Character_Io is
function Get return Character;
-- reads a charactex from the terminal
procedure Flush;
-- throw away all character on the current line
Io_Error : exception;
end Character_Io;
Another package Look contains the function Read which scans the current input line looking for the punctuation characters comma (,) period (.) and semicolon (;). The function will return the next punctuation character found or raise the exception Illegal_Punctuation if a non-alphanumeric character is found. If,
during the process of scanning the input line, an Io_Error is encountered, then the exception is propagated to the caller of Read. The specification of Look is given below.
with Character_Io; use Character_Io;
package Look is
type Punctuation is (Comma, Period, Semicolon);
function Read return Punctuation;
-- reads the next , . or ; from the terminal
Illegal_Punctuation : exception;
end Look;

Sketch the package body of Look using the Character_Io package for reading characters from the terminal. On receipt of a legal punctuation character, an illegal punctuation character or an Io_Error exception, the remainder of the input line should be discarded. You may assume that an input line will always have a legal or illegal punctuation character and that Io_Errors occur at random. Using the Look package, sketch the code of a procedure Get_Punctuation which will always return the next punctuation character in spite of the exceptions that Look raises. An infinite input stream may be assumed.

Check back soon!
02:13

Problem 4

In a process control application, gas is heated in an enclosed chamber. The chamber is surrounded by a coolant which reduces the temperature of the gas by conduction. There is also a valve which when open releases the gas into the atmosphere. The operation of the process is controlled by an Ada package whose specification is given below. For safety reasons, the package recognizes several error conditions; these are brought to the notice of the user of the package by the raising of exceptions. The exception Heater_Stuck_on is raised by the procedure Heater_off when it is unable to turn the heater off. The exception Temperature_Still_Rising is raised by the Increase_Coolant procedure if it is unable to decrease the temperature of the gas by increasing the flow of the coolant. Finally, the exception Valve_Stuck is raised by the Open_Valve procedure if it is unable to release the gas into the atmosphere.
package Temperature_Control is
Heater_Stuck_On, Temperature_Still_Rising,
Valve_Stuck : exception;
procedure Heater_on;
-- turn on heater
procedure Heater_off;
-- turn off heater
-- raises Heater_Stuck_On
procedure Increase_Coolant;
-- Causes the flow of coolant which surrounds the
- chamber to increase until the temperature reaches
-- a safe level raises Temperature_Still_Rising
procedure Open_Valve;
-- opens a valve to release some of the gas thereby
-- avoiding an explosion
-- raises Valve_Stuck
procedure Panic;
-- sounds an alarm and calls the fire,
-- hospital and police services
end Temperature_Control;

Write an Ada procedure which when called will attempt to turn off the heater in the gas chamber. If the heater is stuck on then the flow of coolant surrounding the chamber should be increased. If the temperature still rises then the escape valve should be opened to release the gas. If this fails then the alarm must be sounded and the emergency services informed.

Lottie Adams
Lottie Adams
Numerade Educator

Problem 5

Write a general-purpose generic Ada package to implement nested recovery blocks. (Hint - the primary, secondary modules, acceptance test and so on, should be encapsulated in a package and passed as a parameter to the generic.)

Check back soon!

Problem 6

To what extent could the Ada exception-handling facilities be implemented outside the language by a standard package?

Check back soon!

Problem 7

How would you defend the statement that an Ada exception is a goto where the destination is undeterminable and the source is unknown? Would the same argument hold for (a) the resumption model of exception handling and (b) the CHILL termination model?

Check back soon!

Problem 8

Compare the exception domains of the following apparently identical pieces of code (the variable Initial is of type integer).
procedure Do_Something is
subtype Small_Int is Integer range -16..15;
A : Small_Int := Initial;
begin
...
end Do_something;
procedure Do_Something is
subtype Sma11_Int is Integer range -16.15 ;
A : Small_Int;
begin
A : = Initial;
$\cdots$
end Do_Something ;
procedure Do something is
subtype Small_Int is Integer range -16..15;
A : Small_Int;
begin
begin
A := Initial;
...
end;
end Do_Something;

Check back soon!
04:34

Problem 9

Show how the $\mathrm{C}$ macros given in Section 3.3.3 can be implemented.

Chris Trentman
Chris Trentman
Numerade Educator

Problem 10

Consider the following program:
I : Integer : $=1$;
J : Integer : $=0$;
$\mathrm{K}$ : Integer : $=3$;
procedure Primary is
begin
J $:=20$;
I $:=\mathrm{K} * \mathrm{~J} / 2$
end Primary;
procedure Secondary is
begin
I : = J;
$\mathrm{K}:=4$;
end Secondary;
procedure Tertiary is
begin
J $:=20 ;$
I $:=\mathrm{K} * \mathrm{~J} / 2$
end Tertiary;
This code is to be executed in a recovery block environment and in an exceptionhandling environment. The following is the recovery block environment:
ensure $I=20$
by
Primary;
else by
Secondary;
else by
Tertiary;
else
I $:=20$;
end;
The following is the exception-handling environment:

Failed : exception;
type Module is (P,S, T);
for Try in Module loop
begin
case Try is
when $\mathrm{P} \Rightarrow$
Primary ;
if $I /=20$ then
raise Failed;
end if;
exit ;
when $S \Rightarrow$
Secondary ;
if $I /=20$ then
raise Failed;
end if;
exit;
when $\mathrm{T}=>$
Tertiary;
if $I /=20$ then
raise Failed;
end if;
exit;
end case;
exception
when Failed =>
if $\operatorname{Try}=\mathrm{T}$ then
I $:=20$;
end if;
end;
end loop;

Assuming the termination model of exception handing, compare and contrast the execution of both the recovery block environment and the exception-handling environment code fragments.

Check back soon!
05:10

Problem 11

Illustrate how recovery blocks can be implemented using C++ and Jave. Hint: see Rubira-Calsavara and Stroud (1994).

Khoobchandra Agrawal
Khoobchandra Agrawal
Numerade Educator

Problem 12

Redo the answer to Exercise 3.3 using Java.

Check back soon!

Problem 13

Redo the answer to Exercise 3.4 using Java.

Check back soon!

Problem 14

Explain under what circumstances when others (e:Exception_Id) in Ada is not equivalent to catch (Exception e) in Java.

Check back soon!

Problem 15

What are the advantages and disadvantages of having exceptions integrated into the OOP model?

Check back soon!

Problem 16

A subset of Ada has been defined for use in safety-critical embedded systems. The language has formal semantics and a set of tools that allow static analysis techniques to be performed. It has no exception-handling facility. The designers argue that exceptions cannot occur if the program has been proved correct. What arguments can be put forward for the introduction of exception-handling facilities into the language?

Check back soon!