How to obtain an MException in a cleanup function
이전 댓글 표시
Hi all,
I've noticed that "lasterror" indicates that it will be obsolete soon, and MException.last indications that it can only be used from the command line. Without using a try/catch construct in the calling/parent function that is being cleaned up, is there any good way to check to see if the calling function being cleaned up generated an error?
댓글 수: 14
Adam Danz
2020년 10월 26일
Wouldn't the dbstack or the 'stack' field of the MException list what you're looking for?
Matt
2020년 10월 26일
Bruno Luong
2020년 10월 26일
TMW recommends using try/catch to get the error. What is the reason to prevent you to follow their recommendation?
Matt
2020년 10월 26일
Bruno Luong
2020년 10월 26일
편집: Bruno Luong
2020년 10월 26일
"Having to do a try catch as well as onCleanup is undesirable"
Why?
I never though they usage must be exclusive. In fact they do different things. I usually use them like this:
- onCleanup performs clean-up tasks, usulally implemented in the calling function, it implements the tasks that must be carried-out where as the function terminates normally or terminates because of and error; and
- try/catch is on the caller, to ignore/log/display a warning message if error occurs in the calling function, and eventually branch to different part of the code.
So I never wonder for onCleanup to knows whereas error occurs and what kind of error. That's the job of try/catch.
Now if you don't wan't to use both, then I would say, too bad for you to limit yourself with such constraint.
DBSTOP is for debugging, this is another topic in my book. I'm not sure I understand your description of the inconvenient of debugging flow and exception handling in regular flow of the code.
Rik
2020년 10월 26일
I'm a bit concerned about the removal of lasterror. I want my code to be compatible with Matlab 6.5 (R13), because it is lightning fast in some situations. If lasterror is removed I will be forced to use this mess:
ME='initialise ME as char';
try
something_that_may_error
catch ME; %#ok<NOSEL>
% ^ suppress output in case of ML6.5 (and add the %#ok pragma for mlint)
if isa(ME,'char'),ME=lasterror;end %#ok<LERR>
end
Using onCleanup will not prevent the error from being thrown.
For example,
cleanup = onCleanup(@()disp(dbstack()));
t = 5 * randi(100);
z = t+x; % x is not defined; hence, error.
So it's still not clear to me what the use case is here.
Matt
2020년 10월 26일
Bruno Luong
2020년 10월 26일
편집: Bruno Luong
2020년 10월 26일
try
dosomebuggycode
catch ME
rethrow(ME) % <- here you have you error code, and debugger stops here is wished
% you can also comment temporary try/catch during debugging
% please explain why it doesn't fit your need???
% how lasterror() can help you more than this???
end
Steven Lord
2020년 10월 27일
In the pattern you describe, a try / catch block would be my choice of approaches to use. You only want to log the error data if an error actually occurs and you only want to log the error if it comes from the scope of your function. As written your myNiceOnCleanup function will always get executed, error or no error. In addition there's a chance (albeit a slim one) it could catch an error from a timer object's TimerFcn, a Handle Graphics callback function, etc. [I'm not sure offhand how or if lasterror interacts with a parallel.ThreadPool but I wouldn't be surprised if "complicated" appears more than once in the answer.]
function myNiceFunction(usersBuggyFunctionHandle)
%Do Some setup
try
outputData = usersBuggyFunctionHandle();
%<--- User needs to be able to natively debug this with dbstop if error
catch errorData
myNiceOnCleanup(errorData)
end
end
function myNiceOnCleanup(errorData)
% Process the MException
logErrorData(errorData)
rethrow(errorData) % or throwAsCaller
end
As for debugging this, if an error occurs the user can set a breakpoint on the line inside the catch where myNiceOnCleanup is called. Or maybe this function accepts an optional input and you have a section in your code that calls keyboard if it is specified.
Matt
2020년 10월 27일
Steven Lord
2020년 10월 27일
If you want to unconditionally stop in the catch block, add a call to keyboard.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Advanced Evaluation and Exception Handling에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!