How to "dbup" to ORIGINAL error after try/catch and rethrow?
    5 views (last 30 days)
  
       Show older comments
    
Hi,
When I debug my code, I use "dbstop if error" to inspect the workspace of the function that created an error.
Now, if an error is caught in a try/catch statement and then rethrown, the "dbstop if error" brings me to the workspace of the catch block in which the rethrow happened. From there, I would like to have a way to get back to the workspace in which the original, caught error happened.
Googling suggests that there is no solution (<http://stackoverflow.com/questions/10429229/debugging-back-to-original-error-with-conditional-exception-handling-i-e-rethr here>) but I want to ask here, too. This is a hugely annoying problem and if MathWorks has not addressed it yet, I definitely hope they will do so soon.
If this is indeed impossible, how do I best deal with such a situation? In my case, I need the try/catch to execute some "cleanup" code after an error before Matlab enters debug mode because my error-causing code has control of the keyboard and screen and I cannot use the computer while it is being executed.
0 Comments
Answers (3)
  Jan
      
      
 on 4 Jan 2014
        
      Edited: Jan
      
      
 on 5 Jan 2014
  
      Would this help:
dbstop if all error
?
[EDITED, Thanks Per] In modern Matlab versions this is called:
dbstop if caught error
The former syntax seems to be supported for backward compatibility.
4 Comments
  per isakson
      
      
 on 6 Jan 2014
				
      Edited: per isakson
      
      
 on 6 Jan 2014
  
			Overloading error with a sub-function in the m-file (see my answer) is a "general solution". Yes indeed, it smells and it requires write permissions to the m-files, but until The MathWorks (or someone else) provides something better.
  per isakson
      
      
 on 5 Jan 2014
        
      Edited: per isakson
      
      
 on 6 Jan 2014
  
      "This is a hugely annoying problem". Yes indeed!
I have a hard time to figure out how dbstop if caught error error_id is supposed to be used. This is the best I can think of:
- set dbstop if error and dbstop if caught error error_id
- run test_excc
- Matlab breaks at line 25 sub_test()
- select sub_test() % Step In doesn't work here(?)
- right click and Run Selected
- Matlab breaks at line 39 error('test:error', 'Noooo!');
and the workspace of sub_sub_test() is available for inspection and debugging, i.e. "the ORIGINAL error". Why doesn't Matlab go directly and break at error('test:error', 'Noooo!'); in sub_sub_test() when I run test_excc?
My command window:
    >> dbclear all
    >> dbstop if error
    >> dbstop if caught error test:error
    >> test_excc
    Caught-error breakpoint was hit in test_excc at line 25. The error was:
    Error using test_excc>sub_sub_test (line 39)
    Noooo!
    25          sub_test()
    K>> sub_test
    Error using test_excc>sub_sub_test (line 39)
    Noooo!
    Error in test_excc>sub_test (line 35)
        sub_sub_test(); 
    39          error('test:error', 'Noooo!');
    K>>
where (in one m-file) is copied from your link to stackoverflow
    function test_excc
    %   doc says:
    %       dbstop if error MATLAB:ls:InputsMustBeStrings pauses execution at
    %       the first run-time error outside a try/catch block that has a
    %       message ID of MATLAB:ls:InputsMustBeStrings. 
    %
    %       dbstop if caught error — Run-time error that occurs within the
    %       try portion of a try/catch block. If you want execution to stop 
    %       only if a specific error occurs, specify the message id. See the 
    %       error condition for an example of specifying a message id.
    %
    %       (12) DBSTOP if caught error
    %       (16) DBSTOP if caught error IDENTIFIER
        try
            sub_test()
        catch ME
            if strcmp(ME.identifier, 'test:notsobad')
                fprintf(1, 'Fine\n');
            else
                ME.rethrow();
            end
        end
    end
    function sub_test
        sub_sub_test();
    end
    function sub_sub_test()
        if rand>0.5
            error('test:error', 'Noooo!');
        else
            error('test:notsobad', 'That''OK');
        end
    end
.
[Two hours later:]
Not for faint-hearted!
- Overload the function error temporarily, see below.
- run test_excc
- Matlab breaks in the overloaded function, error
- Step Out takes you to "the ORIGINAL error"
and the workspace of sub_sub_test() is available for inspection and debugging.
My command window:
    >> dbclear all
    >> test_excc
    3   end
    K>>
where
    function    error( varargin )
        dbs
    end
and
    function dbs
    % dbs provides a persistent break-point; debug, dbstop
    %   Based on the FEX contribution kdb 
    %   By Romesh Abeysuriya 15-11-12
        stk = dbstack('-completenames');
        dbstop( 'in', stk(2).file, 'at', num2str( stk(2).line+1 ) )
        pause(0.2)
    end
and don't forget to delete the file, error.m
... or maybe better, add the function error as a sub-function to the file that you are debugging.
0 Comments
  Steven Lord
    
      
 on 22 Aug 2021
        
      Edited: Steven Lord
    
      
 on 22 Aug 2021
  
      Set a breakpoint inside the catch section of the try / catch block. If you only want to enter debug mode when certain errors are thrown and caught, set a conditional breakpoint that stops only if the identifier field of the MException object you caught matches the one you want to debug.
try
    x = ones(3) + ones(4);
catch ME
    fprintf("The error with identifier '%s'\nand message '%s' was thrown.", ...
        ME.identifier, ME.message)
    % or you could use if
    if isequal(ME.identifier, 'MATLAB:sizeDimensionsMustMatch')
        fprintf("You're trying to add arrays with incompatible dimensions.")
    end
end
0 Comments
See Also
Categories
				Find more on Debugging and Analysis in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


