Programming
try
again'l
.", ,
Finally
we
can write the procedure
to
print
all
the address labels'
proe
despatch
cLs
aLL
doLabeL
endaLt
endproc
Leaving the Program The
final
option
IS
to
leave
the program when
you
have finished. This procedure can
be very simple
-
all
It
has
to
do
IS
to
make sure that the
file
is
closed properly before
returning control
to
the keyboard.
We
have also added a short sign·off message
to
make
it
clear that the program has ended.
proe
bye
cLose
print
"bye"
stop
endproc
ERRORS
It
is
qUite
likely that sooner or later you
will
make
an
error while
uSing
this program
You
may,
for example,
aCCidentally
press
the
ESC
key
or
you
may type
in
soine
text
when a number
is
expected.
ThiS
type
of
mistake
IS
detected by Archive and normally
results
in
the
display
of
an
error message and a
return
from
your program
to
the keyboard.
You
can
use
the error command
to
mark a procedure
to
be treated specially
If
any error
is
detected. Any error occumng
In
the marked procedure, or any procedure that
It
calls,
results
in
an immediate, premature, return.
The normal method
of
handling errors
is
switched
all
for
the marked procedure and
it
is
left
to
you
to
decide how
to
deal with
it
You
can find out the number of the last
error that occurred by using the errnum( ) function.
You
can use
It
to
read the error
number more than once
as
the value
is
only cleared
to
zero
by
the next use of the
error command.
If
no errors have occurred since the start
of
the program, or
Since
the
last
time error
was
executed, then errnum( )
Will
return a value of
zero.
This method, although not easy
to
understand
at
first,
gives
you
a very powerful and
flexible control
of
how
to
deal
With
errors. The
follOWing
example shows a typical
way
of
uSing
error.
It
gives you an error·resistant method of inputting a number.
proc
dotest
input
x
endproc
proc
test
Let n
=1
whiLe
n
error
dot
est
Let
n
=errnumO
if
n
print
"You made
error
number
II
in
endif
endwhiLe
endproc
The
first
procedure Simply waits
for
your Input
to
the variable
x.
The second procedure
handles any error during the execution of the Input procedure.
If
any error occurs within
dotest
It
will
be terminated prematurely and the error number
Will
be set This number
is
then read by errnum( ) and,
if it
is
non-zero, the error message
is
printed
(thiS
error
message could,
of
course, be anything
you
like).
Since these statements are enclosed
in
a while endwhile loop, any error
Will
cause them
to
be executed again. The error
number
is
cleared by error, ready for the next
try.
You
can not leave test until you have
typed
in
a valid number
This example reports the number of the error that
was
detected. On most occasions
you
will
not be concerned about which error occurred. The main use of errnum( )
is
to
differentiate between there being no error and there being a detected error of any
type. A
list
of error numbers and
pOSSible
explanations
is
included
in
the Reference
chapter
We
can now write a procedure which will allow you
to
select
anyone
of the
SIX
options
with a single keypress.
It
IS
suffiCiently simple that
no
explanation
IS
necessary.
I
'--/
26
12
B-1