Serial catch catch block for matlab

I would like to run a few lines of code, but I'm not sure if any line will cause an error. However, if an error occurs, I would like the script to ignore this line and continue.

One option is to have a block try-catch-endthat skips a block of code that can cause errors. However, as soon as an error occurs, the rest of the code after the error in the try-statement is not executed.

TL TR: Do I have a different choice than writing a block try-catch-endfor each individual line in the following code example?

Code example:

try
  disp('1st line');
  disp('2nd line');
  PRODUCE_ERROR;  %throws an error, variable/function does not exist
  disp('3rd line'); %%%%%
  disp('4th line'); % these lines I would like to keep executing
  disp('5th line'); %%%%%
catch
  disp('something unexpected happened');
end

Output:

1st line
2nd line
something unexpected happened

The result that would be preferable:

1st line
2nd line
something unexpected happened
3rd line
4th line
5th line

related: Why shouldn't I wrap each block in a "try" - "catch"?

+4
2

, . :

fcnList = {@() disp('1'); ...
           @() disp('2'); ...
           @() error(); ...    % Third function throws an error
           @() disp('4')};

for fcnIndex = 1:numel(fcnList)
  try
    fcnList{fcnIndex}();  % Evaluate each function
  catch
    fprintf('Error with function %d.\n', fcnIndex);  % Display when an error happens
  end
end

, , , :

1
2
Error with function 3.
4

, , , . , , . :

function fcn1
  b = a+1;     % Increments a
  fprintf('%d\n', b);
end
function fcn2
  error();     % Errors
end
function fcn3
  b = a.^2;    % Squares a
  fprintf('%d\n', b);
end

a = 2;
fcnList = {@fcn1 @fcn2 @fcn3};

for fcnIndex = 1:numel(fcnList)
  try
    fcnList{fcnIndex}();
  catch
    fprintf('Error with function %d.\n', fcnIndex);
  end
end

:

3
Error with function 2.
4
+4

script . , script, , (, for end , ...). , , , , .

:

function execute_script(fname)
fid = fopen(fname,'rt');
n = 0;
while ~feof(fid)
   cmd = fgetl(fid);
   n = n+1;
   if ~isempty(cmd)
      try
         evalin('caller',cmd);
      catch exception
         disp(['Error occurred executing line number ',num2str(n),': ',exception.message]);
      end
   end
end

, : , evalin . . .

, testscript.m :

A = 1;
B = 2+C; % This line needs a variable not defined in the script!
D = 5;

MATLAB:

>> execute_script('testscript.m')
Error occurred executing line number 2: Undefined function or variable 'C'.
>> whos
  Name      Size            Bytes  Class         Attributes

  A         1x1                 8  double                  
  D         1x1                 8  double                  

A D. C:

>> C=0;
>> execute_script('testscript.m')
>> whos
  Name      Size            Bytes  Class         Attributes

  A         1x1                 8  double                  
  B         1x1                 8  double                  
  C         1x1                 8  double                  
  D         1x1                 8  double                  

C script B.

+1

Source: https://habr.com/ru/post/1685690/


All Articles