<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../style.css">
<title>
Gambas Documentation - CATCH
</title>
</head>
<table class="none" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td align="left">
<font size="-1">
<a href="../../help+en"><img class="flag" alt="Home" border="0" src="../../img/lang/en.png" align="center"></a>&nbsp;
<a href="../lang+en">Up</a>&nbsp;
<a href="case+en">Previous</a>&nbsp;
<a href="cbool+en">Next</a>&nbsp;
</td></tr></table>
<div class="notab">
<h1>
CATCH
</h1>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax">SUB Function ( ... )
&nbsp;&nbsp;...
<b>CATCH</b>
&nbsp;&nbsp;...
END</pre><p>

This instruction indicates the beginning of the error management part of a function or procedure.
<p>
The catch part is executed when an error is raised between the beginning of the function execution and its end. This error can be raised by the function itself, or by any other function called during its execution, provided that this deeper function has no catch part itself&nbsp;: the deeper the catch part is, the more priority it has.
<p>
If an error is raised during the execution of the catch part, it is normally propagated. The catch part does not protect itself&nbsp;!
<p>
If there is a finally part in the function, it must precede the catch part. See <a href="finally+en">FINALLY</a>   for more details.
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">' Print a file to the screen

SUB PrintFile(FileName AS STRING)

  DIM hFile AS File
  DIM sLig AS STRING

  OPEN FileName FOR READ AS #hFile

  WHILE NOT EOF(hFile)
    LINE INPUT #hFile, sLig
    PRINT sLig
  WEND

FINALLY ' Always executed, even if a error raised

  CLOSE #hFile

CATCH ' Executed only if there is an error

  PRINT &quot;Cannot print file &quot;; FileName

END</pre>
<p>
<hr><b>See also</b><br>
<a href="../cat/error+en">Error Management</a>&nbsp;

</div>
</body>
</html>

