Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 1519:3ebf9781707c | 1520:d9a5405a3102 |
|---|---|
| 50 <li><a href="#blocks">Blocks</a></li> | 50 <li><a href="#blocks">Blocks</a></li> |
| 51 <li><a href="#chunks">Chunks</a></li> | 51 <li><a href="#chunks">Chunks</a></li> |
| 52 <li><a href="#assignment">Assignment</a></li> | 52 <li><a href="#assignment">Assignment</a></li> |
| 53 <li><a href="#control">Control Structures</a></li> | 53 <li><a href="#control">Control Structures</a></li> |
| 54 <li><a href="#for">For Statement</a></li> | 54 <li><a href="#for">For Statement</a></li> |
| 55 <li><a href="#try">Try Statement</a></li> | |
| 55 <li><a href="#fn_stmt">Function Calls as Statements</a></li> | 56 <li><a href="#fn_stmt">Function Calls as Statements</a></li> |
| 56 <li><a href="#local_stmt">Local Declarations</a></li> | 57 <li><a href="#local_stmt">Local Declarations</a></li> |
| 57 <li><a href="#template_stmt">Template Statements</a></li> | 58 <li><a href="#template_stmt">Template Statements</a></li> |
| 58 </ul> | 59 </ul> |
| 59 </li> | 60 </li> |
| 241 | 242 |
| 242 <p> | 243 <p> |
| 243 Luan code can explicitly generate an error by calling the | 244 Luan code can explicitly generate an error by calling the |
| 244 <a href="#Luan.error"><code>error</code></a> function. | 245 <a href="#Luan.error"><code>error</code></a> function. |
| 245 If you need to catch errors in Luan, | 246 If you need to catch errors in Luan, |
| 246 you can use <a href="#Luan.pcall"><code>pcall</code></a> or <a href="#Luan.try"><code>try</code></a> | 247 you can use the <a href="#try">Try Statement</code></a>. |
| 247 to call a given function in <em>protected mode</em>. | |
| 248 | 248 |
| 249 | 249 |
| 250 <p> | 250 <p> |
| 251 Whenever there is an error, | 251 Whenever there is an error, |
| 252 an <em>error table</em> | 252 an <em>error table</em> |
| 1039 | 1039 |
| 1040 </ul> | 1040 </ul> |
| 1041 | 1041 |
| 1042 | 1042 |
| 1043 | 1043 |
| 1044 <h4 heading><a name="try" href="#for">Try Statement</a></h4> | |
| 1045 | |
| 1046 <p>The <b>try</b> statement has the same semantics as in Java.</p> | |
| 1047 | |
| 1048 <pre> | |
| 1049 stat ::= <b>try</b> block [<b>catch</b> Name block] [<b>finally</b> block] end_try | |
| 1050 end_try ::= <b>end_try</b> | <b>end</b> | |
| 1051 </pre> | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1044 | 1055 |
| 1045 <h4 heading><a name="fn_stmt" href="#fn_stmt">Function Calls as Statements</a></h4> | 1056 <h4 heading><a name="fn_stmt" href="#fn_stmt">Function Calls as Statements</a></h4> |
| 1046 | 1057 |
| 1047 <p> | 1058 <p> |
| 1048 To allow possible side-effects, | 1059 To allow possible side-effects, |
| 1938 <p> | 1949 <p> |
| 1939 will iterate over all key–value pairs of table <code>t</code>. | 1950 will iterate over all key–value pairs of table <code>t</code>. |
| 1940 | 1951 |
| 1941 | 1952 |
| 1942 | 1953 |
| 1943 <h4 heading><a name="Luan.pcall" href="#Luan.pcall"><code>Luan.pcall (f [, arg1, ···])</code></a></h4> | |
| 1944 | |
| 1945 <p> | |
| 1946 Calls function <code>f</code> with | |
| 1947 the given arguments in <em>protected mode</em>. | |
| 1948 This means that any error inside <code>f</code> is not propagated; | |
| 1949 instead, <code>pcall</code> catches the error | |
| 1950 and returns a status code. | |
| 1951 Its first result is the status code (a boolean), | |
| 1952 which is true if the call succeeds without errors. | |
| 1953 In such case, <code>pcall</code> also returns all results from the call, | |
| 1954 after this first result. | |
| 1955 In case of any error, <code>pcall</code> returns <b>false</b> plus the error. | |
| 1956 | |
| 1957 | |
| 1958 | |
| 1959 | |
| 1960 <p> | 1954 <p> |
| 1961 <hr><h3><a name="pdf-print"><code>print (···)</code></a></h3> | 1955 <hr><h3><a name="pdf-print"><code>print (···)</code></a></h3> |
| 1962 Receives any number of arguments | 1956 Receives any number of arguments |
| 1963 and prints their values to <code>stdout</code>, | 1957 and prints their values to <code>stdout</code>, |
| 1964 using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert each argument to a string. | 1958 using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert each argument to a string. |
| 2072 If the metatable of <code>v</code> has a <code>"__to_string"</code> field, | 2066 If the metatable of <code>v</code> has a <code>"__to_string"</code> field, |
| 2073 then <code>to_string</code> calls the corresponding value | 2067 then <code>to_string</code> calls the corresponding value |
| 2074 with <code>v</code> as argument, | 2068 with <code>v</code> as argument, |
| 2075 and uses the result of the call as its result. | 2069 and uses the result of the call as its result. |
| 2076 | 2070 |
| 2077 | |
| 2078 | |
| 2079 <h4 heading><a name="Luan.try" href="#Luan.try"><code>Luan.try (t, ···)</code></a></h4> | |
| 2080 | |
| 2081 <p> | |
| 2082 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. | |
| 2083 | |
| 2084 <p> | |
| 2085 Example use: | |
| 2086 | |
| 2087 <pre> | |
| 2088 try { | |
| 2089 function() | |
| 2090 a_dangerous_fn() | |
| 2091 end | |
| 2092 catch = function(e) | |
| 2093 -- handle error | |
| 2094 end | |
| 2095 finally = function() | |
| 2096 -- clean up | |
| 2097 end | |
| 2098 } | |
| 2099 </pre> | |
| 2100 | |
| 2101 <p> | |
| 2102 Could be defined as: | |
| 2103 | |
| 2104 <pre> | |
| 2105 function Luan.try(t) | |
| 2106 local r = { <a href="#Luan.pcall">Luan.pcall</a>(t[1]) } | |
| 2107 if r[1] then | |
| 2108 Table.remove(r,1) | |
| 2109 elseif t.catch ~= nil then | |
| 2110 r = { t.catch(r[2]) } | |
| 2111 else | |
| 2112 t.finally and t.finally() | |
| 2113 r[2].throw() | |
| 2114 end | |
| 2115 t.finally and t.finally() | |
| 2116 return Table.unpack(r) | |
| 2117 end | |
| 2118 </pre> | |
| 2119 | 2071 |
| 2120 | 2072 |
| 2121 <h4 heading><a name="Luan.type" href="#Luan.type"><code>Luan.type (v)</code></a></h4> | 2073 <h4 heading><a name="Luan.type" href="#Luan.type"><code>Luan.type (v)</code></a></h4> |
| 2122 | 2074 |
| 2123 <p> | 2075 <p> |
