How to implement try-catch in separate functions (called and caller), but still access the workspace of the called function.
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm attempting to implement more intuitive error handling in some software I'm working on. Previously, my base function called a subfunction for user input. The subfunction contained simple logical tests for error checking, and would return an empty set and return prematurely if an error occurred. The base function would then check if the return value was empty, report an error, then exit prematurely as well. I had essentially implemented an error flag. I attempted to improve this process by using try-catch statements. As I wanted the entire program to stop execution when an unresolvable error occurred, I placed my catch statements in the base function, and my try functions in the subfunction. This had the intended effect of prematurely ending the subfunction and returning control to the base function when an error is detected. However, it also had the effect of changing the stack behaviour of built-in errors. Below is are the try and catch statements from the base function and subfunction.
Base function:
% if M not defined, get from user.
if ~exist('M','var')
try
M = model_select(F);
catch err
switch err.identifier
case {'model_select:noFile','model_select:wrongFeature'}
fprintf('%sExiting\n',err.message);
return;
otherwise
rethrow(err);
end
end
end
subfunction (model_select.m):
% Throw exception if no files selected
if isnumeric(file)
err.identifier = 'model_select:noFile';
err.message = sprintf('Model Selection: No files were selected\n');
error(err);
return;
end
As I mentioned, built in errors, such as those generated from syntax errors, were no longer debuggable. Enabling "dbstop if error" would normally result in the stack being displayed, and workspace traversal up and down for all levels. After implementing my try-catch statements, errors in the subfunction were being caught in the main function. The stack still included line numbers to the location of any errors in the subfunction, but I was no longer able to traverse to the subfunction's workspace to view the values present. Is there a better way to implement try-catch that will allow me to view the subfunction's workspace for debugging, but still allow premature returns without flagging?
Edit: Changed the title to better reflect what I was asking.
0 comentarios
Respuestas (0)
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!