在调用堆栈中处理异常的 Matlab 最小工作示例?

Matlab minimum working example of handling exception higher up in call stack?

我一直在浏览 Matlab 的异常处理页面,但很难找到一个完整的最小工作示例,说明由异常生成位置上方的调用堆栈中的函数处理的异常。任何人都可以指向这样的页面,或确认它缺少吗?

谢谢。

我想到了类似以下最小工作示例的内容,展示了将错误从被调用函数传递给调用者的机制。 TMW 同意像这样的东西可以填补他们文档中的空白(他们协助创建了这个例子)。

% main.m
%-------
fprintf(1,'----------------\nRunning foo(1,2)\n')
try
   foo(1,2)
catch ME0
   ME0
end

fprintf(1,'\n');

fprintf(1,'----------------\nRunning foo(1,[2 3])\n')
try
   foo(1,[2 3])
catch ME0
   ME0
end

fprintf(1,'----------------\nRunning foo(0,4)\n')
try
   foo(0,4)
catch ME0
   ME0
end


% foo.m
%------
function foo(A,B)
   try
      barDbar(A,B)
   catch ME1
      fprintf(1,'\n');
      fprintf(1,'Running catch block in %s\n',mfilename)
      ME1, drawnow('update') % Doesn't always flush stdout !!
      % No `throw` command, error doesn't propagate to main as ME0
   end
end

% barDbar.m
%----------
function barDbar(A,B)
   try
      foobar(A,B);      
   catch ME2
      fprintf(1,'\n');
      fprintf(1,'Running catch block in %s\n',mfilename)
      ME2, drawnow('update') % Doesn't always flush stdout !!
      throw(ME2) % Required to "escalate" error to caller
   end
end

% foobar.m
%---------
function V = foobar(V1,V2)
   V = cat(1,V1,V2);               

   fprintf(1,'\n');

   % The following only executes if `cat` does *not* encounter
   % an error
   fprintf(1,'%s encountered no error.\n',mfilename)
   if V1 == 0
      fprintf(1,'Creating artificial exception instead.\n')
      ME = MException( 'foobar:ArtificalException' , ...
                       'foobar added artifical exception' );
      ME, drawnow('update') % Doesn't always flush stdout !!
      throw( ME )
   end % if
end

输出:

----------------
Running foo(1,2)

foobar encountered no error.

----------------
Running foo(1,[2 3])

Running catch block in barDbar
ME2 = 
  MException with properties:

    identifier: 'MATLAB:catenate:dimensionMismatch'
       message: 'Dimensions of matrices being concatenated are not consistent.'
         cause: {0x1 cell}
         stack: [4x1 struct]

Running catch block in foo
ME1 = 
  MException with properties:

    identifier: 'MATLAB:catenate:dimensionMismatch'
       message: 'Dimensions of matrices being concatenated are not consistent.'
         cause: {0x1 cell}
         stack: [3x1 struct]
----------------
Running foo(0,4)

foobar encountered no error.
Creating artificial exception instead.
ME = 
  MException with properties:

    identifier: 'foobar:ArtificalException'
       message: 'foobar added artifical exception'
         cause: {}
         stack: [0x1 struct]

Running catch block in barDbar
ME2 = 
  MException with properties:

    identifier: 'foobar:ArtificalException'
       message: 'foobar added artifical exception'
         cause: {}
         stack: [4x1 struct]

Running catch block in foo
ME1 = 
  MException with properties:

    identifier: 'foobar:ArtificalException'
       message: 'foobar added artifical exception'
         cause: {}
         stack: [3x1 struct]

TMW 的 Capture Information About Exceptions page 展示了如何在每个级别(即每个 try/catch 块)创建一个新的异常并从较低级别的函数附加异常(而不是仅仅向上传播这些异常).

我从这个实验中发现的一件事是异常对象不会自动级联到每个级别的异常对象链中。这必须手动完成。

另一个小细节是,即使 MException 可以制造异常,它与实际错误(或使用 error 创建的错误)的不同之处在于它仍然需要显式抛出.相反,抛出是由实际错误和使用 error.

创建的错误自动完成的