Errors only display [1x1] stack trace despite error being thrown within a class method
10 ビュー (過去 30 日間)
古いコメントを表示
Christopher Minasi
2022 年 2 月 24 日
回答済み: Christopher Minasi
2022 年 3 月 1 日
Core Issue:
On line 68 of my script, when I run:
output = classObj.method();
I get the following error:
Error using wrapper (line 68)
Index in position 1 exceeds array bounds.
Expected error:
Index in position 1 exceeds array bounds.
Error in Class.method (line 13)
label = obj.labels(index, 1);
Error in wrapper (line 68)
output = classObj.method();
The issue is that no stack trace is being displayed.
-------------------------------------
What I've done to investigate:
Looking at the MException object (using the lasterror function), I see that MException.stack is a 1x1 struct that looks like this:
'C:\Users\User\Documents\wrapper.m' 'wrapper' 68
With Identifier: 'MATLAB:badsubscript'
When I catch the error from within the method, like so:
try
%Method code here
catch e
assignin('base','withinMethodError',e);
throw(e)
end
The withinMethodError MException object's stack is a 3x1 struct that looks like this:
'C:\Users\User\Documents\Class.m' 'Class.method' 657
'C:\Program Files\MATLAB\R2021b\toolbox\matlab\lang\+matlab\+internal\+lang\fwdSubsref.m' 'fwdSubsref' 6
'C:\Users\User\Documents\wrapper.m' 'wrapper' 68
but the thrown error/ MException object is still only showing the wrapper line instead of the entire trace.
This issue applied to all methods of the class and does not apply to function file. Even with the exact same function that made up the method, which I pulled out into its own methodFunc.m file & ran:
output = methodFunc(classObj)
which gave me an error similar to what I expected in the first place:
Index in position 1 exceeds array bounds.
Error in methodFunc (line 13)
label = obj.labels(index, 1);
Error in wrapper (line 68)
output = methodFunc(classObj);
This issue is also reproduced by causing an error by inserting assert(0) into the method, and by using a try catch in the wrapper function in addition to having try catch within the method.
Additional Info:
This was never an issue for me in using Matlab for over a year, this has only come up directly after a fresh install of matlab on a new Windows 11 computer (my previous computer's OS was Windows 10). I tried uninstalling and reinstalling matlab, which did not solve the issue.
I could just add a try catch to every method I use then save the object using assignin, or could avoid methods entirely, but I find methods to be a very convenient and organized way to write code, so I'd very much appreciate a solution that solves the core issue rather than providing a workaround.
Thanks,
Chris
2 件のコメント
採用された回答
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!