Errors only display [1x1] stack trace despite error being thrown within a class method

10 views (last 30 days)
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 Comments
Christopher Minasi
Christopher Minasi on 24 Feb 2022
Edited: Christopher Minasi on 24 Feb 2022
I figured that since the error was being caught, that it was the first time throwing the error, but I suppose I should still use rethrow when using try-catch? I went ahead and just tried using rethrow and the same issue persists unfortunately :/ thanks for the suggestion though!

Sign in to comment.

Accepted Answer

Christopher Minasi
Christopher Minasi on 1 Mar 2022
Alright, so I figured out the issue while trying to create a minimal reproducible example, and realized that I left out an important detail in this question. The issue arises only when assigning the class object to a containers.Map object. To avoid this issue, assign the object to a variable just before calling a method, as shown in the example below:
Saved in its own Class.m file:
classdef Class
%CLASS Summary of this class goes here
% Detailed explanation goes here
properties
Property1
end
methods
function obj = Class(inputArg1,inputArg2)
%CLASS Construct an instance of this class
% Detailed explanation goes here
obj.Property1 = inputArg1 + inputArg2;
end
function outputArg = method1(obj,inputArg)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
assert(0, 'internal error')
outputArg = obj.Property1 + inputArg;
end
end
end
Saved in a seperate wrapper.m file:
example = 2;
if example == 1
%error displays correctly in this example
classObjs = containers.Map;
classObjs('a') = Class(1,2);
classObjs('b') = Class(3,4);
classObj = classObjs('a');
out = classObj.method1(1);
else
%stack trace doesn't work in this example
classObjs = containers.Map;
classObjs('a') = Class(1,2);
classObjs('b') = Class(3,4);
out = classObjs('a').method1(1);
end
I suppose that not many people map objects this way (I was using it for looping & code simplification purposes), but hopefully this can save somebody some time in the future. And while low priority/ likely more trouble than its worth, maybe this can be fixed in a future update?
-Chris

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!