<HTML> <HEAD> <!-- Created by texi2html 1.56k + clip patches and <A href="http://www.clip.dia.fi.upm.es/Software">lpdoc</A> from ciao.texi on 28 January 2007 --> <LINK rel="stylesheet" href="ciao.css" type="text/css"> <TITLE>The Ciao Prolog System - Exception handling</TITLE> </HEAD> <BODY> Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_25.html">previous</A>, <A HREF="ciao_27.html">next</A>, <A HREF="ciao_241.html">last</A> section, <A HREF="ciao_toc.html">table of contents</A>. <P><HR><P> <H1><A NAME="SEC160" HREF="ciao_toc.html#TOC160">Exception handling</A></H1> <P> <A NAME="IDX1823"></A> <P> <STRONG>Author(s):</STRONG> The CLIP Group. <P> <STRONG>Version:</STRONG> 1.10#7 (2006/4/26, 19:22:13 CEST) <P> <STRONG>Version of last change:</STRONG> 1.9#344 (2004/4/29, 12:56:34 CEST) <P> This module includes predicates related to exceptions, which alter the normal flow of Prolog. <UL> <LI><A HREF="ciao_26.html#SEC161">Usage and interface (exceptions)</A> <LI><A HREF="ciao_26.html#SEC162">Documentation on exports (exceptions)</A> </UL> <H2><A NAME="SEC161" HREF="ciao_toc.html#TOC161">Usage and interface (<CODE>exceptions</CODE>)</A></H2> <div class="cartouche"> <UL> <LI><STRONG>Library usage:</STRONG> These predicates are builtin in Ciao, so nothing special has to be done to use them. <LI><STRONG>Exports:</STRONG> <UL> <LI><EM>Predicates:</EM> <A NAME="IDX1824"></A> <CODE>catch/3</CODE>, <A NAME="IDX1825"></A> <CODE>intercept/3</CODE>, <A NAME="IDX1826"></A> <CODE>throw/1</CODE>, <A NAME="IDX1827"></A> <CODE>halt/0</CODE>, <A NAME="IDX1828"></A> <CODE>halt/1</CODE>, <A NAME="IDX1829"></A> <CODE>abort/0</CODE>. </UL> </UL> </div class="cartouche"> <H2><A NAME="SEC162" HREF="ciao_toc.html#TOC162">Documentation on exports (<CODE>exceptions</CODE>)</A></H2> <P> <A NAME="IDX1830"></A> <A NAME="IDX1831"></A> <DL> <DT><span class="define">PREDICATE:</span> <B>catch/3:</B> <DD><A NAME="IDX1832"></A> <P> <CODE>catch(Goal, Error, Handler)</CODE> <P> Executes <CODE>Goal</CODE>. If an exception is raised during its execution, <CODE>Error</CODE> is unified with the exception, and if the unification succeeds, the entire execution derived from <CODE>Goal</CODE> is aborted, and <CODE>Handler</CODE> is executed. The execution resumes with the continuation of the catch/3 call. For example, given the code <PRE> p(X) :- throw(error), display('---'). p(X) :- display(X). </PRE> <P> the execution of "<CODE>catch(p(0), E, display(E)), display(.), fail.</CODE>" results in the output "<CODE>error.</CODE>". <P> <EM>Meta-predicate</EM> with arguments: <CODE>catch(goal,?,goal)</CODE>. <P> <STRONG>Usage:</STRONG> <CODE>catch(+callable, ?term, +callable)</CODE> <KBD> * ISO * </KBD> <UL> <LI><EM>The following properties hold globally:</EM> This predicate is understood natively by CiaoPP. (<CODE>basic_props:native/1</CODE>) </UL> </DL> <P> <A NAME="IDX1833"></A> <A NAME="IDX1834"></A> <DL> <DT><span class="define">PREDICATE:</span> <B>intercept/3:</B> <DD><A NAME="IDX1835"></A> <P> <CODE>intercept(Goal, Error, Handler)</CODE> <P> Executes <CODE>Goal</CODE>. If an exception is raised during its execution, <CODE>Error</CODE> is unified with the exception, and if the unification succeeds, <CODE>Handler</CODE> is executed and then the execution resumes after the predicate which produced the exception. Note the difference with builtin <A NAME="IDX1836"></A> <CODE>catch/3</CODE>, given the same code defined there, the execution of "<CODE>intercept(p(0), E, display(E)), display(.), fail.</CODE>" results in the output "<CODE>error---.0.</CODE>". <P> <EM>Meta-predicate</EM> with arguments: <CODE>intercept(goal,?,goal)</CODE>. </DL> <P> <A NAME="IDX1837"></A> <A NAME="IDX1838"></A> <DL> <DT><span class="define">PREDICATE:</span> <B>throw/1:</B> <DD><A NAME="IDX1839"></A> <P> <CODE>throw(Ball)</CODE> <P> Raises an error, throwing the exception <CODE>Ball</CODE>, to be caught by an ancestor <A NAME="IDX1840"></A> <CODE>catch/3</CODE> or <A NAME="IDX1841"></A> <CODE>intercept/3</CODE>. The closest matching ancestor is chosen. Exceptions are also thrown by other builtins in case of error. <P> <STRONG>Usage:</STRONG> <KBD> * ISO * </KBD> <UL> <LI><EM>Calls should, and exit will be compatible with:</EM> <CODE>Ball</CODE> is currently a term which is not a free variable. (<CODE>term_typing:nonvar/1</CODE>) </UL> </DL> <P> <A NAME="IDX1842"></A> <A NAME="IDX1843"></A> <DL> <DT><span class="define">PREDICATE:</span> <B>halt/0:</B> <DD><A NAME="IDX1844"></A> <P> <CODE>halt(halt</CODE> <P> Halt the system, exiting to the invoking shell. <P> <STRONG>Usage:</STRONG> <KBD> * ISO * </KBD> <UL> <LI><EM>The following properties hold globally:</EM> This predicate is understood natively by CiaoPP. (<CODE>basic_props:native/1</CODE>) </UL> </DL> <P> <A NAME="IDX1845"></A> <A NAME="IDX1846"></A> <DL> <DT><span class="define">PREDICATE:</span> <B>halt/1:</B> <DD><A NAME="IDX1847"></A> <P> <CODE>halt(Code)</CODE> <P> Halt the system, exiting to the invoking shell, returning exit code <CODE>Code</CODE>. </DL> <P> <A NAME="IDX1848"></A> <A NAME="IDX1849"></A> <DL> <DT><span class="define">PREDICATE:</span> <B>abort/0:</B> <DD><A NAME="IDX1850"></A> <P> <CODE>abort(abort</CODE> <P> Abort the current execution. </DL> <P><HR><P> Go to the <A HREF="ciao_1.html">first</A>, <A HREF="ciao_25.html">previous</A>, <A HREF="ciao_27.html">next</A>, <A HREF="ciao_241.html">last</A> section, <A HREF="ciao_toc.html">table of contents</A>. </BODY> </HTML>