Try catch error function not working

11 visualizaciones (últimos 30 días)
Uyen Nguyen
Uyen Nguyen el 26 de Sept. de 2020
Comentada: Walter Roberson el 26 de Sept. de 2020
I am having a hard time using the try catch error function to catch any negative input value for base (b) and height(h). I want to only take in positive value as input and takes the absolute value of the input if it's negative and an error message is displayed. Please help
if app.ShapeDropDown.Value == "Rectangle"
% taking in the dimensions of base and height
% Catching resulting errors if the input values are invalid
try
b = app.Dimension1EditField.Value;
h = app.Dimension2EditField.Value;
catch ME
if b < 0 | h <0
msgID = 'MATLAb:InvalidInput';
msg = 'Input for dimension(s) must be positive';
baseException = MException(msgID,msg);
baseException = addCause(baseException, causeExption);
b = abs(b);
h = abs(h);
end
% Calculate MOI
Ix = (b*(h^3))/12;
Iy = ((b^3)*h)/12;
% Displau the MOI on the table
d = {'Base',b, RegUnit;'Height',h,RegUnit;'Ix',Ix, CalcUnit;'Iy',Iy, CalcUnit'};
fig = uifigure('Position',[100 100 752 250]);
uit = uitable('Parent',fig,'Position',[25 50 700 200]);
uit.Data = d;
% Create axes for the figure. Axes of figure changes
% depending on base and height entered
app.UIAxes.XLim = [-1 b+1.00];
app.UIAxes.YLim = [-1 h+1.00];
%Polar axes
XX = linspace(b/2, Ix, 500);
YY = linspace(h/2, Iy, 500);
% % Create a vector that would span the X & Y direction
% positioned at the center of base and height
IXline = ones(1,500)*h/2;
IYline = ones(1,500)*b/2;
% Graph the rectangular shape on the axes
rectangle(app.UIAxes,'Position',[0 0 b h])
hold(app.UIAxes);%hold on
% plot the polar axes
plot(app.UIAxes,XX,IXline, 'r')
plot(app.UIAxes,IYline, YY, 'k')
hold(app.UIAxes,'off')
end

Respuestas (1)

Cris LaPierre
Cris LaPierre el 26 de Sept. de 2020
Editada: Cris LaPierre el 26 de Sept. de 2020
Your code must generate an error in the 'try' portion before it will execute what is in the catch. I don't see any reason why the current 'try' code should ever generate an error. Therefore, the code in 'catch' never runs.
Try looking at some of the examples in the try-catch documentation page.
  1 comentario
Walter Roberson
Walter Roberson el 26 de Sept. de 2020
That try/catch can produce an exception -- if app.Dimension1EditField.Value or app.Dimension2EditField.Value do not exist.
However, to have the catch invoked under the circumstance if b < 0 | h <0 then you would need to insert into the try section code such as
if b < 0 | h <0
error("values out of range");
end
and remove the "if" from the catch.
... But if you were going to do that, there would not be much point in doing so when you could simply put the if into the main code without any try/catch.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by