Matlab在调用堆栈中处理异常更高的最小工作示例? [英] Matlab minimum working example of handling exception higher up in call stack?

查看:101
本文介绍了Matlab在调用堆栈中处理异常更高的最小工作示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览Matlab的异常处理页面,在查找异常的完整最小工作示例方面有些困难,该示例由异常生成上方的调用堆栈中的函数处理。任何人都可以指向这样的页面,或确认缺少该页面吗?

I've been browsing Matlab's exception handling pages, and having a bit of difficulty finding a complete minimum working example of an exception being handled by a function in the call stack that is above where the exception is generated. Would anyone be able to point to such a page, or confirm that it is lacking?

谢谢。

推荐答案

我想到的是以下最小工作示例,它显示了将错误从已调用函数传递给调用方的机制。 TMW同意,类似的东西将填补他们文档中的空白(他们帮助创建了这个示例)。

I had in mind something like the following minimum working example, showing the mechanics in handing an error from an invoked function to a caller. TMW agrees that something like this would fill in a gap in their documentation (they assisted in creating this example).

% 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的关于异常的捕获信息页面显示了如何在每个级别(即每个try / catch块)上创建一个新的异常并从较低级别的函数追加异常(而不是仅仅向上传播那些异常)。

TMW's Capture Information About Exceptions page shows how, at each level (i.e., each try/catch block), one can create a new exception and append exceptions from lower level functions (instead of just propagating those exceptions upward).

我从该实验中发现的一件事是异常对象不要自动在每个级别上级联成一个异常对象链。

One of the things I found from this experiment was that the exception objects don't automatically get cascaded into a chain of exception objects at each level. This has to be done manually.

另一个小细节是,即使 MException 可以产生异常,它也可以与实际错误(或由 error 创建的错误)的不同之处在于,仍然需要显式地抛出它。相反,抛出是由实际错误和使用错误创建的错误自动完成的。

Another little detail is that, even though MException can manufacture an exception, it differs from an actual error (or an error created with error) in that it still needs to be thrown explicitly. In contrast, throwing is done automatically by real errors and errors created using error.

这篇关于Matlab在调用堆栈中处理异常更高的最小工作示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆