Strange dependence of object display behavior on debugger state

1 次查看(过去 30 天)
Consider the following simple classdef,
classdef myclass
properties (Dependent)
data
end
methods
function val=get.data(obj)
error(' ')
end
function keyboard(obj)
keyboard
end
end
end
When I create an object, and try to display it at the command line, I get a different response, depending on the debugger state, as illustrated below.
>>obj=myclass;
>> dbclear all; obj
obj =
myclass with no properties.
>> dbclear all; dbstop if error; obj
obj =
myclass with no properties.
>> dbclear all; dbstop if caught error; obj
obj =
Caught-error breakpoint was hit in myclass>myclass.get.data at line 10. The error was:
Error using myclass/get.data
10 error(' ')
K>>
So it would seem that the default display() method uses try/catch to intercept errors in get.property() methods.
As a side note, I feel it is dangerously misleading for the default display() to hide the error completely and to tell the user that myclass has "no properties" when clearly that is not true. But let's leave that aside for now.
The thing I find really odd is that in the Matlab Editor, when one hovers the mouse over the object to see its properties, the above behavior appears to be reversed. In particular, if we do,
dbclear all; dbstop if caught error; obj.keyboard
and hover the mouse as shown in the screenshot below, we are now told that obj has no properties:
Suddenly, however, dbstop if error is sufficient to intercept the error in get.data().
dbclear all; dbstop if error; obj.keyboard
To reproduce the result in the screenshot below, you must once again hover over obj in Line 14. The debugger will then break at Line 10, as shown below, but only for a short time (about 5 sec).
So, the question. How do we make sense of this behavior, and is it intended?

回答(1 个)

aditi bagora
aditi bagora 2024-5-9
Hello Matt,
I tried to replicate the issue at my end by duplicating the same code and running it in different environments. The code behaves as expected in MATLAB R2023b.
I created the following class "test" and tried to access the data property of the object of the class in three different settings i.e.
  • without debugger
  • dbstop if error
  • dbstop if caught error
In each of these scenarios, I encountered an error, which was in line with my expectations.
Notably, when the debugger paused execution and I attempted to examine the object by hovering over it (thus, trying to access the object), the command window promptly reflected the error.
classdef test
properties(Dependent)
data
end
methods
function val = get.data(obj)
error('Error when getting the object')
end
function keyboard(obj)
keyboard
end
end
end
Please see the attached file, which displays the results obtained from executing commands in the command window.
  1 个评论
Matt J
Matt J 2024-5-9
@aditi bagora the steps shown in your tmp.png screenshot do not mimic my steps. In none of the cases in my post was I invoking the property with direct dot-indexing syntax.
Regarding hovering, I cannot comment because your screenshot does not show the result of that, nor does it make it clear what the debugger state was when you did so.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by