Parse the string try and evaluate it as if it were an Octave program.
If execution fails, evaluate the optional string catch.
The string try is evaluated in the current context, so any results
remain available after eval
returns.
The following example creates the variable A with the approximate value of 3.1416 in the current workspace.
eval ("A = acos(-1);");
If an error occurs during the evaluation of try then the catch string is evaluated, as the following example shows:
eval ('error ("This is a bad example");', 'printf ("This error occurred:\n%s\n", lasterr ());'); -| This error occurred: This is a bad example
Programming Note: if you are only using eval
as an error-capturing
mechanism, rather than for the execution of arbitrary code strings,
Consider using try/catch blocks or unwind_protect/unwind_protect_cleanup
blocks instead. These techniques have higher performance and don’t
introduce the security considerations that the evaluation of arbitrary code
does.
See also: evalin, evalc, assignin, feval.
Package: octave