Global exception handling in a GUI class

2 views (last 30 days)
Gregor
Gregor on 17 Sep 2013
Hi,
I have a class that represents a GUI window and want to display any kind of exception that occurs in any class method as a errordlg (as console output ist not useful when using a gui).
  • Is there a convenient way to redirect all exception-message-strings to the function errordlg()?
  • This should especially include all errors that occur within any class method (like a call to obj.methodXY())
  • It is desireable to catch also other errors (like wrong use of obj(..))
I have already tried overloading B=subsref(A,S), but this causes vast problems when methods with/without input-/output-arguments are called.

Answers (1)

Sean de Wolski
Sean de Wolski on 17 Sep 2013
Edited: Sean de Wolski on 17 Sep 2013
I don't think there is a way to redirect from standard error to error dialog. You'll have to catch the MException and then throw the error dialog from there. This could be packaged as a function to make it reusable.
  2 Comments
Gregor
Gregor on 17 Sep 2013
Yes I have assumed that there is no general function or the like to do this.
Of course I could place try-catch-statements in every single class method to redirect the error to an error dialog. But I want to avoid to do this for every method.
I would like to have something similar to the approach in the following piece of code: the subsref() method does exactly the same as the builtin-subsref method but for redirecting every exception to an error dialog:
class classX < handle
methods
function method1(obj, arg1);
function out1 = method2(obj);
function varargout = subsref(obj, S)
try
varargout = builtin('subsref', obj, S);
catch ex
errordlg(ex.message, ex.identifier);
end function obj = classX()
% constructor code...
endend
end
In this code, the subsref method doesn't perform well with different types of methods.
Sean de Wolski
Sean de Wolski on 17 Sep 2013
I guess I don't understand how subsref would be called from a user using the figure/GUI/app.
Wouldn't whatever the user is doing be safeguarded against at that level? How are they going to use subsref?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!