Home > utils > disperror.m

disperror

PURPOSE ^

DISPERROR Display latest error message and where the error occured.

SYNOPSIS ^

function returnMessage = disperror

DESCRIPTION ^

DISPERROR Display latest error message and where the error occured.
   DISPERROR displays the message of the last error and also where this
   error occured. This is helpful when the error was captured by a try-
   catch block and then rethrown. Matlab will show the error message, but
   unfortunately not where the error occured. DISPERROR displays the error
   message and the error stack, complete with hyperlinks.

   Another use for DISPERROR is with callbacks containing try-catch
   block(s). In this case, however, the call to disperror must be in the
   catch part of a try-catch block, such as:

     set(gcf, 'ButtonDownFcn', 'try;fun;catch disperror;end');
   or
     set(gcf, 'ButtonDownFcn', 'try;fun;catch disperror;rethrow(lasterror);end');

   The latter usage will cause the error message to be repeated, but will also
   give information on which callback that was executed.

   Example:

      function testdisperror
         try
            a=[1 2 3];
            b=a(4711);  % Index error
         catch
            % possibly do some housekeeping here
            rethrow(lasterror);
            end

      >> testdisperror
      ??? Attempted to access a(4711); index out of bounds because numel(a)=3.

      >> disperror
      ??? Attempted to access a(4711); index out of bounds because numel(a)=3.
      Error in ==> testdisperror at 4
                   ------------------  % <- Hyperlink

   =======================
   Version: 1.0 2006-02-27
   Author: Jerker W�berg, More Research, SWEDEN
   email: char(hex2dec(reshape('6A65726B65722E77616762657267406D6F72652E7365',2,[])')')

   8/14/08 - Stefan Tomic - Took out hyperlinks and edited so that
                            it returns the error message as a string.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function returnMessage = disperror
0002 %DISPERROR Display latest error message and where the error occured.
0003 %   DISPERROR displays the message of the last error and also where this
0004 %   error occured. This is helpful when the error was captured by a try-
0005 %   catch block and then rethrown. Matlab will show the error message, but
0006 %   unfortunately not where the error occured. DISPERROR displays the error
0007 %   message and the error stack, complete with hyperlinks.
0008 %
0009 %   Another use for DISPERROR is with callbacks containing try-catch
0010 %   block(s). In this case, however, the call to disperror must be in the
0011 %   catch part of a try-catch block, such as:
0012 %
0013 %     set(gcf, 'ButtonDownFcn', 'try;fun;catch disperror;end');
0014 %   or
0015 %     set(gcf, 'ButtonDownFcn', 'try;fun;catch disperror;rethrow(lasterror);end');
0016 %
0017 %   The latter usage will cause the error message to be repeated, but will also
0018 %   give information on which callback that was executed.
0019 %
0020 %   Example:
0021 %
0022 %      function testdisperror
0023 %         try
0024 %            a=[1 2 3];
0025 %            b=a(4711);  % Index error
0026 %         catch
0027 %            % possibly do some housekeeping here
0028 %            rethrow(lasterror);
0029 %            end
0030 %
0031 %      >> testdisperror
0032 %      ??? Attempted to access a(4711); index out of bounds because numel(a)=3.
0033 %
0034 %      >> disperror
0035 %      ??? Attempted to access a(4711); index out of bounds because numel(a)=3.
0036 %      Error in ==> testdisperror at 4
0037 %                   ------------------  % <- Hyperlink
0038 %
0039 %   =======================
0040 %   Version: 1.0 2006-02-27
0041 %   Author: Jerker W�berg, More Research, SWEDEN
0042 %   email: char(hex2dec(reshape('6A65726B65722E77616762657267406D6F72652E7365',2,[])')')
0043 %
0044 %   8/14/08 - Stefan Tomic - Took out hyperlinks and edited so that
0045 %                            it returns the error message as a string.
0046     err = lasterror;
0047     % See if it is a syntax error
0048     match=regexp(err.message ...
0049             , {'(?<=File: *)([^ ].*)(?= *Line:)','(?<=Line: *)\d+'} ...
0050             , 'match');
0051     sp=1;
0052     mf=[];
0053     if any(cellfun('isempty',match))
0054         % Not syntax error, mimic Matlabs first error line
0055         returnMessage = sprintf(['??? ' err.message '\n']);
0056         if length(err.stack)>0
0057             [mf,fn,line]=editem(err.stack(1));
0058             sp=2;
0059         end
0060     else
0061         % Syntax error, first line of message contains info for hyperlink
0062         mf=char(match{1});
0063         line=str2num(char(match{2}));
0064         % Show the actual error
0065         returnMessage = sprintf(['??? ' ...
0066             char(regexp(err.message,'(?<=\n)(.*)','match')) '\n']);
0067         [fn,fn]=fileparts(mf);
0068         end
0069     if ~isempty(mf)
0070         % Show the error stack
0071         returnMessage = [returnMessage ...
0072                  (sprintf('Error in ==> %s at %d\n',fn,line))];
0073         for i=sp:length(err.stack)
0074             [mf,fn,line]=editem(err.stack(i));
0075             returnMessage = [returnMessage ...
0076                      (sprintf('  In %s at %d\n',fn,line))];
0077         end
0078     end
0079 
0080 function [mf,fn,line]=editem(x)
0081     % Build the function name
0082     [p,mnam]=fileparts(x.file);
0083     fn=x.name;
0084     if ~strcmpi(regexp(fn,'^[^/]+', 'match'),mnam)
0085         fn = [mnam '>' fn];
0086         end
0087     mf=x.file;
0088     line=x.line;

Generated on Wed 20-Mar-2019 04:00:51 by m2html © 2003