Scilab Home page | Wiki | Bug tracker | Forge | Mailing list archives | ATOMS | File exchange
Please login or create an account
Change language to: Français - Português - 日本語 - Русский

Please note that the recommended version of Scilab is 6.0.1. This page might be outdated.
See the recommended documentation of this function

Scilab Help >> Scilab > Scilab keywords > try

try

beginning of try block in try-catch control instruction

catch

beginning of catch block in try-catch control instruction

Calling Sequence

try
statements
catch
statements
end

Description

The try-catch control instruction can be used to manage codes that could possibly generate errors.

When a try-catch control instruction is executed, normally only the statements between the try and 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 the catch and end keywords are executed using the default error handling mode (see errcatch).

The recorded error can be retrieved using the lasterror function.

The 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 to:

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 try-catch control structures. This context is detected and produces a specific error message (this error is caught and stored like any other error if it is triggered in the try block).

However, try-catch control structures can be nested (see example 2 below).

Examples

// 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")

See Also

  • error — error message
  • execstr — execute Scilab code in strings
  • if — keyword for conditional execution
  • lasterror — get last recorded error message
  • errcatch — error trapping
Scilab Enterprises
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Wed Apr 01 10:13:49 CEST 2015