Mercurial Hosting > luan
diff website/src/manual.html @ 1520:d9a5405a3102
try statement
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 21 Jun 2020 18:14:13 -0600 |
parents | 56fb5cd8228d |
children | d3e61cd2aca0 |
line wrap: on
line diff
--- a/website/src/manual.html Fri Jun 19 20:10:47 2020 -0600 +++ b/website/src/manual.html Sun Jun 21 18:14:13 2020 -0600 @@ -52,6 +52,7 @@ <li><a href="#assignment">Assignment</a></li> <li><a href="#control">Control Structures</a></li> <li><a href="#for">For Statement</a></li> + <li><a href="#try">Try Statement</a></li> <li><a href="#fn_stmt">Function Calls as Statements</a></li> <li><a href="#local_stmt">Local Declarations</a></li> <li><a href="#template_stmt">Template Statements</a></li> @@ -243,8 +244,7 @@ Luan code can explicitly generate an error by calling the <a href="#Luan.error"><code>error</code></a> function. If you need to catch errors in Luan, -you can use <a href="#Luan.pcall"><code>pcall</code></a> or <a href="#Luan.try"><code>try</code></a> -to call a given function in <em>protected mode</em>. +you can use the <a href="#try">Try Statement</code></a>. <p> @@ -1041,6 +1041,17 @@ +<h4 heading><a name="try" href="#for">Try Statement</a></h4> + +<p>The <b>try</b> statement has the same semantics as in Java.</p> + +<pre> + stat ::= <b>try</b> block [<b>catch</b> Name block] [<b>finally</b> block] end_try + end_try ::= <b>end_try</b> | <b>end</b> +</pre> + + + <h4 heading><a name="fn_stmt" href="#fn_stmt">Function Calls as Statements</a></h4> @@ -1940,23 +1951,6 @@ -<h4 heading><a name="Luan.pcall" href="#Luan.pcall"><code>Luan.pcall (f [, arg1, ···])</code></a></h4> - -<p> -Calls function <code>f</code> with -the given arguments in <em>protected mode</em>. -This means that any error inside <code>f</code> is not propagated; -instead, <code>pcall</code> catches the error -and returns a status code. -Its first result is the status code (a boolean), -which is true if the call succeeds without errors. -In such case, <code>pcall</code> also returns all results from the call, -after this first result. -In case of any error, <code>pcall</code> returns <b>false</b> plus the error. - - - - <p> <hr><h3><a name="pdf-print"><code>print (···)</code></a></h3> Receives any number of arguments @@ -2076,48 +2070,6 @@ -<h4 heading><a name="Luan.try" href="#Luan.try"><code>Luan.try (t, ···)</code></a></h4> - -<p> -Implements try-catch as found in other languages where each block is in table <code>t</code>. <code>t[1]</code> is the "try" block. The <code>t.catch</code> and <code>t.finally</code> blocks are optional. Any extra arguments are passed to the "try" function. Returns the result of the "try" block or the "catch" block. - -<p> -Example use: - -<pre> -try { - function() - a_dangerous_fn() - end - catch = function(e) - -- handle error - end - finally = function() - -- clean up - end -} -</pre> - -<p> -Could be defined as: - -<pre> - function Luan.try(t) - local r = { <a href="#Luan.pcall">Luan.pcall</a>(t[1]) } - if r[1] then - Table.remove(r,1) - elseif t.catch ~= nil then - r = { t.catch(r[2]) } - else - t.finally and t.finally() - r[2].throw() - end - t.finally and t.finally() - return Table.unpack(r) - end -</pre> - - <h4 heading><a name="Luan.type" href="#Luan.type"><code>Luan.type (v)</code></a></h4> <p>