1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
<?xml version="1.0" encoding="UTF-8"?>
<refentry version="5.0-subset Scilab" xml:id="try" xml:lang="en"
xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:ns5="http://www.w3.org/1999/xhtml"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:db="http://docbook.org/ns/docbook">
<info>
<pubdate>$LastChangedDate$</pubdate>
</info>
<refnamediv>
<refname>try</refname>
<refpurpose>beginning of try block in try-catch control
instruction</refpurpose>
</refnamediv>
<refnamediv xml:id="catch">
<refname>catch</refname>
<refpurpose>beginning of catch block in try-catch control
instruction</refpurpose>
</refnamediv>
<refsynopsisdiv>
<title>Calling Sequence</title>
<synopsis>try
statements
catch
statements
end</synopsis>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>The <literal>try</literal>-<literal>catch</literal> control
instruction can be used to manage codes that could possibly generate
errors.</para>
<para>When a <literal>try</literal>-<literal>catch</literal> control
instruction is executed, normally only the statements between the
<literal>try</literal> and <literal>catch</literal> keywords are executed.
However, if an error occurs during execution of any of these statements,
the error is recorded, the remaining statements up to the
<literal>catch</literal> keyword are skipped and the statements between
the <literal>catch</literal> and <literal>end</literal> keywords are
executed using the default error handling mode (see <link
linkend="errcatch">errcatch</link>).</para>
<para>The recorded error can be retreived using the <link
linkend="lasterror">lasterror</link> function.</para>
<para>The <literal>catch</literal> statements as well as the
<literal>catch</literal> keyword can be omitted if no alternative
statements are given.</para>
<para>Note that one can also use the <link
linkend="execstr">execstr</link> function with
<literal>'errcatch'</literal> argument for error handling. This can be
particularly useful for handling syntactical errors.</para>
<para>Note also that <literal>try-catch</literal> is more or less similar
to:</para>
<programlisting role = ""><![CDATA[
if execstr("<try instructions>","errcatch")<>0 then
<catch instructions>
end
]]></programlisting>
<para>It uses the same internal mechanism as <link
linkend="errcatch">errcatch</link>. It is the reason why <link
linkend="errcatch">errcatch</link> or
<emphasis>execstr(...,"errcatch")</emphasis> cannot be included in
<literal>try</literal>-<literal>catch</literal> control structures. This
context is detected and produces a specific error message (this error is
catched and stored like any other error if it is triggered in the
<literal>try</literal> block).</para>
<para>However, <literal>try</literal>-<literal>catch</literal> control
structures can be nested (see example 2 below).</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting role="example"><![CDATA[
// example 1
file_path=TMPDIR+'/wrong'
try
u=mopen(file_path,'r')
x=mget(10,'c',u)
catch
disp(['file '+file_path+ 'cannot be read',
'using default values for x'])
x=1:10
end
[error_message,error_number]=lasterror(%t)
// example 2 (nested try/catch structures)
function nestedtry(a,b)
disp("START")
mprintf("\ta is %s\t\tb is %s\n",string(a),string(b))
try
disp("try 1")
try
disp("try 2")
z=a+1; // err when string
catch
disp("catch 2")
t=b+1; // err when string
end
disp("after try 2")
catch
disp("catch 1")
end
disp("after try 1 - THE END")
endfunction
nestedtry(1,1)
nestedtry("a string",1)
nestedtry(1,"a string")
nestedtry("a string","a string")
]]></programlisting>
</refsection>
<refsection>
<title>See Also</title>
<simplelist type="inline">
<member><link linkend="error">error</link></member>
<member><link linkend="execstr">execstr</link></member>
<member><link linkend="if">if</link></member>
<member><link linkend="lasterror">lasterror</link></member>
<member><link linkend="errcatch">errcatch</link></member>
</simplelist>
</refsection>
<refsection>
<title>Authors</title>
<para>Serge Steer, INRIA</para>
</refsection>
</refentry>
|