MException fallback for Matlab 6.5
1 view (last 30 days)
Show older comments
The new MException object for try catch expressions is very nice. Unfortunately I still have to support Matlab 6.5. Does anybody use a workaround, such that catch ME is still valid in the historic Matlab versions?
0 Comments
Accepted Answer
Walter Roberson
on 14 Jan 2012
If you have a mechanism for defining a function or not depending on the version, then you could try:
...
catch ME
ME_TOO = ME; %the copy is important
...
disp(ME_TOO.message); %for example
end
%only build this in if MException is not supported
function Exception = ME
persistent LastException
if nargout == 0
LastException = lasterror();
LastException.cause = {};
end
Exception = LastException;
So when ME appears on the 'catch' statement then the function recognizes it was called with no outputs and records the lasterror. When the function appears in the assignment, it sees the output exists and copies out the remembered exception.
Copying a normal MException object is not a problem. Copying with this replacement gives you a struct whose fields can be accessed (whereas you cannot directly access the fields of a structure returned by a function.)
5 Comments
More Answers (1)
Jan
on 28 Jan 2012
7 Comments
Daniel Shub
on 30 Jan 2012
@Jan, Thank you for that. I keep learning new things about how badly behaved timer objects are.
See Also
Categories
Find more on Error Handling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!