\section{TRY-CATCH Try and Catch Statement}

\subsection{Usage}

The \verb|try| and \verb|catch| statements are used for error handling
and control.  A concept present in \verb|C++|, the \verb|try| and \verb|catch|
statements are used with two statement blocks as follows
\begin{verbatim}
   try
     statements_1
   catch
     statements_2
   end
\end{verbatim}
The meaning of this construction is: try to execute \verb|statements_1|,
and if any errors occur during the execution, then execute the
code in \verb|statements_2|.  An error can either be a FreeMat generated
error (such as a syntax error in the use of a built in function), or
an error raised with the \verb|error| command.
\subsection{Examples}

Here is an example of a function that uses error control via \verb|try|
and \verb|catch| to check for failures in \verb|fopen|.
\begin{verbatim}
    read_file.m
function c = read_file(filename)
try
   fp = fopen(filename,'r');
   c = fgetline(fp);
   fclose(fp);
catch
   c = ['could not open file because of error :' lasterr]
end
\end{verbatim}
Now we try it on an example file - first one that does not exist,
and then on one that we create (so that we know it exists).
\begin{verbatim}
--> read_file('this_filename_is_invalid')
c = 
  <string>  - size: [1 107]
 could not open file because of error :No such file or directory for fopen argument thi
 s_filename_is_invalid
ans = 
  <string>  - size: [1 107]
 could not open file because of error :No such file or directory for fopen argument thi
 s_filename_is_invalid
--> fp = fopen('test_text.txt','w');
--> fprintf(fp,'a line of text\n');
--> fclose(fp);
--> read_file('test_text.txt')
ans = 
  <string>  - size: [1 15]
 a line of text


\end{verbatim}
