-\section{Extension {\tt trywith}: exceptions and finalization}\r
-Lua offers error handling primitives \verb+pcall()+ and\r
-\veb+xpcall()+. However, they are pretty low level, and their syntax\r
-is cumbersome to use and read back. This extension offers a proper\r
-syntax for the handling of such exceptions.\r
-\r
-\subsection{Syntax}\r
-An error handling statement has the following form:\r
-\r
-\begin{verbatim}\r
-try\r
- <protected block>\r
-catch <exception pattern #1> then\r
- <exception handling block #1>\r
-catch <exception pattern #2> then\r
- <exception handling block #2>\r
- ...\r
-catch <exception pattern #n> then\r
- <exception handling block #n>\r
-finally\r
- <finalization block>\r
-end\r
-\end{verbatim}\r
-\r
-\subsection{Semantics}\r
-When such a statement is executed:\r
-\begin{itemize}\r
-\item the protected code is executed\r
-\item if its execution causes an error, the error message is matched\r
- against all exception patterns, until one matches or the last one is\r
- reached (see the \verb+match+ extension for patterns\r
- semantics). Patterns can include guard clauses. The block\r
- corresponding to the first matching pattern is executed. If no\r
- pattern matches, the error will be rethrown.\r
-\item the block following the \verb+finally+ keyword will be executed\r
- no matter what: if the protected block executes succesfully, if it\r
- raises an error, if it causes a \verb+return+, if an error occurs in\r
- an exception handling block, whether an error is caught or\r
- not... The only reason why the finalization block might not be run\r
- is because of a sudden death of the process (call to {\tt os.exit()}\r
- or core dump).\r
-\end{itemize}\r
-\r
-The finally block can be omitted, or there can be no error catching\r
-case. The following examples are legal:\r
-\r
-\begin{verbatim}\r
-try\r
- f = io.open ('file.txt', 'r')\r
- num_char = #f:read '*a'\r
-finally\r
- f:close()\r
-end\r
-\r
-try\r
- do_stuff()\r
-catch "mismatch" then\r
- print "match statement failure"\r
-catch "[Ee]of"/_ then -- regexp pattern\r
- print "An end-of-file error seems to have happened"\r
-catch x if type(x)=='table' then\r
- print "The error is a table, not a string!"\r
-end\r
-\end{verbatim}\r
-\r
- \subsection{RAII}\r
- RAII, or ``Resource Acquisition Is Initialization'', is a common\r
- programming pattern in which an object's lifetime is strictly\r
- associated with a given lexical scope. For instance, if a file is\r
- opened in a given scope, it must be closed as soon as this scope is\r
- leaved, even if it's leaved due to a {\tt return} or an error. The\r
- ``finally'' block allows this, but since it's a very common use case,\r
- there is a dedicated extension ``with/do'': you initialize some resource\r
- behind the ``with'' keyword, and it will be closed after the ``do''\r
- block is left. The only constraint is that the resources must have a\r
- {\tt:close()} method which releases them. Here is a usage example:\r
-\r
-\begin{verbatim}\r
--{ extension 'withdo' }\r
-\r
-with f1, f2 = io.open 'file1.txt', io.open 'file2.txt' do\r
- local t1 = f1:read '*a'\r
- local t2 = f2:read '*a'\r
- printf("The files contain %i and %i chars respectively", t1, t2)\r
-end\r
-\end{verbatim}\r
-\r
+\section{Extension {\tt trywith}: exceptions and finalization}
+Lua offers error handling primitives \verb+pcall()+ and
+\veb+xpcall()+. However, they are pretty low level, and their syntax
+is cumbersome to use and read back. This extension offers a proper
+syntax for the handling of such exceptions.
+
+\subsection{Syntax}
+An error handling statement has the following form:
+
+\begin{verbatim}
+try
+ <protected block>
+catch <exception pattern #1> then
+ <exception handling block #1>
+catch <exception pattern #2> then
+ <exception handling block #2>
+ ...
+catch <exception pattern #n> then
+ <exception handling block #n>
+finally
+ <finalization block>
+end
+\end{verbatim}
+
+\subsection{Semantics}
+When such a statement is executed:
+\begin{itemize}
+\item the protected code is executed
+\item if its execution causes an error, the error message is matched
+ against all exception patterns, until one matches or the last one is
+ reached (see the \verb+match+ extension for patterns
+ semantics). Patterns can include guard clauses. The block
+ corresponding to the first matching pattern is executed. If no
+ pattern matches, the error will be rethrown.
+\item the block following the \verb+finally+ keyword will be executed
+ no matter what: if the protected block executes succesfully, if it
+ raises an error, if it causes a \verb+return+, if an error occurs in
+ an exception handling block, whether an error is caught or
+ not... The only reason why the finalization block might not be run
+ is because of a sudden death of the process (call to {\tt os.exit()}
+ or core dump).
+\end{itemize}
+
+The finally block can be omitted, or there can be no error catching
+case. The following examples are legal:
+
+\begin{verbatim}
+try
+ f = io.open ('file.txt', 'r')
+ num_char = #f:read '*a'
+finally
+ f:close()
+end
+
+try
+ do_stuff()
+catch "mismatch" then
+ print "match statement failure"
+catch "[Ee]of"/_ then -- regexp pattern
+ print "An end-of-file error seems to have happened"
+catch x if type(x)=='table' then
+ print "The error is a table, not a string!"
+end
+\end{verbatim}
+
+ \subsection{RAII}
+ RAII, or ``Resource Acquisition Is Initialization'', is a common
+ programming pattern in which an object's lifetime is strictly
+ associated with a given lexical scope. For instance, if a file is
+ opened in a given scope, it must be closed as soon as this scope is
+ leaved, even if it's leaved due to a {\tt return} or an error. The
+ ``finally'' block allows this, but since it's a very common use case,
+ there is a dedicated extension ``with/do'': you initialize some resource
+ behind the ``with'' keyword, and it will be closed after the ``do''
+ block is left. The only constraint is that the resources must have a
+ {\tt:close()} method which releases them. Here is a usage example:
+
+\begin{verbatim}
+-{ extension 'withdo' }
+
+with f1, f2 = io.open 'file1.txt', io.open 'file2.txt' do
+ local t1 = f1:read '*a'
+ local t2 = f2:read '*a'
+ printf("The files contain %i and %i chars respectively", t1, t2)
+end
+\end{verbatim}
+