Title: Exception Handling, Modules, Names
1Exception Handling,Modules, Names
2Exception Handling
- An exception is any expression E. To raise the
exception , one executes the following statement - raise E end
- proc Eval E ?R case E of plus(X Y) then
Browse XY times(X Y) then Browse XY
else raise illFormedExpression(E) end endend - The basic exception handling statement is called
a try-statement - try S catch Pattern1 then S1 Pattern2 then
S2 Patternn then Snend
3Semantics
- Execution is equivalent to executing S if S does
not raise an exception. - If S raises exception E, and E matches one of the
patterns Patterni, control is passed to the
corresponding statement Si. - If E does not match any pattern the exception is
propagated outside the try-statement until
eventually caught by the system, which catches
all escaped exceptions. - try S catch Pattern1 then S1 Pattern2 then
S2 Patternn then Snend
4Example
- try ForAll plus(5 10) times(6 11)
min(7 10) Evalcatch illFormedExpression(X)
then Browse X end
5try catch finally
- A try-statement may also specify a final
statement Sfinal, which is executed on normal as
well as on exceptional exit. try S catch
Pattern1 then S1 Pattern2 then S2
Patternn then Snfinally Sfinalend - Example
- Assume that F is an opened file the procedure
Process/1 manipulates the file in some way and
the procedure CloseFile/1 closes the file. - The following program ensures that the F is
closed upon normal or exceptional exit.try
Process Fcatch illFormedExpression(X) then
Browse X finally CloseFile F end
6System Exceptions
- The exceptions raised by the Oz system are
records with one of the labels failure, error,
and system. - failure indicates the attempt to perform an
inconsistent equality operation on the store of
Oz. - error indicates a runtime error which should not
occur such as applying a nonprocedure to some
argument or adding an integer to an atom, etc. - system indicates a runtime condition because of
the environment of the Mozart operating system
process, i.e., an unforeseeable situation like a
closed file or window or failing to open a
connection between two Mozart processes.
7- proc One X X1 end
- proc Two X X2 end
- try OneTwo
- catch failure(...) then Show caughtFailure
- end
- Here the pattern failure(...) catches any record
whose label is failure. - When an exception is raised but not handled, an
error message is printed in the emulator window
(standard error), and the current thread
terminates. - In stand-alone applications the default behavior
is that a message is printed on standard error
and the whole application terminates. It is
possible to change this behavior to something
else that is more desirable for particular
applications.
8Modules and Interfaces
- A module is a software component that is
first-class. - A module consists of a collection of Oz entities
(procedure, objects and other values) that are
constructed together to provide certain related
functionality. - A module typically has a number of private
entities that are not visible outside the module
and a interface the external services of the
module. - The interface provides the services of the
module. - The lexical scoping of the language and the
record data-type suffices to construct modules.
9Homemade Module
- declare List local proc Append ... ... e
nd proc Partition ... ... end proc So
rt ... ... Partition ... ... end proc Mem
ber ... ... end in List 'export'(append
Append sort Sort
member Member ... )end - Browse List.append 1 2 3 3 4
private
10Functors, Modules and Interfaces
functor export appendAppendsortSortmember
Memberdefine proc Append ... ... end proc
Partition ... ... end proc Sort ...
... Partition ... ... end proc Member ... ..
. end end stored at /home/person/List.oz
compiled to a compiled-functor at file
/home/person/List.ozf
- Modules are software components that may be
stateful - Functors are specification of modules (stateless)
- A functor specifies how to create module from
other modules (import), the interface of a module
(export), and the initialization procedure
(define).
11Functors, Modules and Interfaces
- Functors as first class values
define functor LFexport appendAppendsortSort
memberMemberdefine proc Append ... ... end
proc Partition ... ... end proc Sort ...
... Partition ... ... end proc Member ..
. ... end end declare List Module.link LF
12Functors
- Linking functors into modules
- declare List Module.link /home/person/list.o
zf - When a functor is linked the statements between
define and end are executed. - Functors may have import declarations
- functor import Browser FO at 'file///h
ome/person/FileOperations.ozf' define Brows
er.browse FO.countLines '/etc/passwd' end - functors are values like any other entity.
13Names for Security
- declare SensitiveNewNameproc Service In
Out case In of Sensitive(X Y) then
service1(X Y) then serviceN(X Y)then
else raise unknownRequest end
endend service1, serviceN are guessable - Sensitive is not