Please note that the recommended version of Scilab is 2023.1.0. This page might be outdated.
See the recommended documentation of this function
beginning of try block in try-catch control instruction
beginning of catch block in try-catch control instruction
try statements catch statements end
instruction can be used to manage codes that could possibly generate
instruction is executed, normally only the statements between the
catch 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
catch keyword are skipped and the statements between
end keywords are
executed using the default error handling mode (see errcatch).
The recorded error can be retreived using the lasterror function.
catch statements as well as the
catch keyword can be omitted if no alternative
statements are given.
Note that one can also use the execstr function with
'errcatch' argument for error handling. This can be
particularly useful for handling syntactical errors.
Note also that
try-catch is more or less similar
if execstr("<try instructions>","errcatch")<>0 then <catch instructions> end
It uses the same internal mechanism as errcatch. It is the reason why errcatch or
execstr(...,"errcatch") cannot be included in
catch 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
structures can be nested (see example 2 below).
// 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")
Serge Steer, INRIA
|<< tilda||Scilab keywords||while >>|