/
html/
<HTML>
<HEAD>
<!-- This HTML file has been created by texi2html 1.51
     from ProgrammersManual.texinfo on 4 March 1997 -->

<TITLE>LambdaMOO Programmer's Manual - Catch</TITLE>
</HEAD>
<BODY>
Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_25.html">previous</A>, <A HREF="ProgrammersManual_27.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>.
<P><HR><P>


<H3><A NAME="SEC26" HREF="ProgrammersManual_toc.html#TOC26">Catching Errors in Expressions</A></H3>

<P>
It is often useful to be able to <STRONG>catch</STRONG> an error that an expression
raises, to keep the error from aborting the whole task, and to keep on running
as if the expression had returned some other value normally.  The following
expression accomplishes this:

</P>

<PRE>
` <VAR>expr-1</VAR> ! <VAR>codes</VAR> =&#62; <VAR>expr-2</VAR> '
</PRE>

<P>
<STRONG>Note:</STRONG> the open- and close-quotation marks in the previous line are
really part of the syntax; you must actually type them as part of your MOO
program for this kind of expression.

</P>
<P>
The <VAR>codes</VAR> part is either the keyword <CODE>ANY</CODE> or else a
comma-separated list of expressions, just like an argument list.  As in an
argument list, the splicing operator (<SAMP>`@'</SAMP>) can be used here.  The
<CODE>=&#62; <VAR>expr-2</VAR></CODE> part of the error-catching expression is optional.

</P>
<P>
First, the <VAR>codes</VAR> part is evaluated, yielding a list of error codes that
should be caught if they're raised; if <VAR>codes</VAR> is <CODE>ANY</CODE>, then it is
equivalent to the list of all possible MOO values.

</P>
<P>
Next, <VAR>expr-1</VAR> is evaluated.  If it evaluates normally, without raising an
error, then its value becomes the value of the entire error-catching
expression.  If evaluating <VAR>expr-1</VAR> results in an error being raised, then
call that error <VAR>E</VAR>.  If <VAR>E</VAR> is in the list resulting from evaluating
<VAR>codes</VAR>, then <VAR>E</VAR> is considered <STRONG>caught</STRONG> by this error-catching
expression.  In such a case, if <VAR>expr-2</VAR> was given, it is evaluated to get
the outcome of the entire error-catching expression; if <VAR>expr-2</VAR> was
omitted, then <VAR>E</VAR> becomes the value of the entire expression.  If <VAR>E</VAR>
is <EM>not</EM> in the list resulting from <VAR>codes</VAR>, then this expression does
not catch the error at all and it continues to be raised, possibly to be caught
by some piece of code either surrounding this expression or higher up on the
verb-call stack.

</P>
<P>
Here are some examples of the use of this kind of expression:

</P>

<PRE>
`x + 1 ! E_TYPE =&#62; 0'
</PRE>

<P>
Returns <CODE>x + 1</CODE> if <CODE>x</CODE> is an integer, returns <CODE>0</CODE> if <CODE>x</CODE> is
not an integer, and raises <CODE>E_VARNF</CODE> if <CODE>x</CODE> doesn't have a value.

</P>

<PRE>
`x.y ! E_PROPNF, E_PERM =&#62; 17'
</PRE>

<P>
Returns <CODE>x.y</CODE> if that doesn't cause an error, <CODE>17</CODE> if <CODE>x</CODE>
doesn't have a <CODE>y</CODE> property or that property isn't readable, and raises
some other kind of error (like <CODE>E_INVIND</CODE>) if <CODE>x.y</CODE> does.

</P>

<PRE>
`1 / 0 ! ANY'
</PRE>

<P>
Returns <CODE>E_DIV</CODE>.

</P>
<P><HR><P>
Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_25.html">previous</A>, <A HREF="ProgrammersManual_27.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>.
</BODY>
</HTML>