Strange dependence of object display behavior on debugger state

1 visualización (últimos 30 días)
Matt J
Matt J el 6 de Mayo de 2024
Comentada: Matt J el 9 de Mayo de 2024
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?

Respuestas (1)

aditi bagora
aditi bagora el 9 de Mayo de 2024
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 comentario
Matt J
Matt J el 9 de Mayo de 2024
@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.

Iniciar sesión para comentar.

Categorías

Más información sobre Software Development Tools en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by