如何使用单行打印捕获的异常的堆栈跟踪?

Dim*_*ims 1 matlab warnings exception try-catch stack-trace

我想捕获一个异常,但是如果没有捕获则会打印它将打印的相同消息(堆栈跟踪).怎么做?

我试过了

>> myfunctionwitherror
Error using myfunctionwitherror (line 3)
myerror

>> try myfunctionwitherror; catch disp(lasterror), end
Warning: This try-catch syntax will continue to work in R2007b, but may be illegal or may mean something different in future releases of MATLAB.
See MATLAB R2007a Release Notes, "Warning Generated by try-catch" for details. 
  MException with properties:

    identifier: ''
       message: 'myerror'
         cause: {0×1 cell}
         stack: [0×1 struct]

>> try myfunctionwitherror, catch e getReport(e), end
Warning: This try-catch syntax will continue to work in R2007b, but may be illegal or may mean something different in future releases of MATLAB.
See MATLAB R2007a Release Notes, "Warning Generated by try-catch" for details. 
Undefined function or variable 'e'.
Run Code Online (Sandbox Code Playgroud)

怎么做到这一点?


我在用2016b.我不知道为什么会出现此消息.

And*_*uri 5

您正在为try catch使用2007a语法.

它是:

try,
 statementA
catch,
 statementB
end
Run Code Online (Sandbox Code Playgroud)

现在是

try
 statementA
catch e
 statementB
end
Run Code Online (Sandbox Code Playgroud)

但是当你正在编写一个衬垫时,你会忘记它,;所以你只是在混淆MATLAB关于线条结束的时候,所以它假设你在做什么

try 
   myfunctionwitherror 
catch 
  e 
  getReport(e)
end
Run Code Online (Sandbox Code Playgroud)

当你使用模糊的一个衬里时,只需将分号放在它们应该的位置.或者写多行.;)

 try; myfunctionwitherror; catch e; getReport(e); end;
Run Code Online (Sandbox Code Playgroud)

如果你想要的只是显示错误(没有错误)

try; myfunctionwitherror; catch e; disp(e.message); end;
Run Code Online (Sandbox Code Playgroud)