Adnan
Adnan

Reputation: 613

Showing complete error stack while error handling

I have a matlab script that calls various other function. I am handling possible error in the following way

            try
                 matStart(MatObj);
             catch err
                 msgbox('Error in Processing Figures!','Error!','error','modal');
                 fprintf(2,err.message);
                 sprintf('\n');
                 display(err.message);
            end

as you can probably guess, this prints the error that caused the exception.But this only prints the very first function that caused the error. I want the whole error stack to be shown down to the last nested function that caused the error to occur. Can tis be done?

Upvotes: 21

Views: 5971

Answers (2)

Edric
Edric

Reputation: 25140

Following on from @thewopr's answer, you can have the text printed in red if you wish by printing the error stack to the 'standard error' output stream, like so:

...
fprintf(2, '%s\n', getReport(err, 'extended'));
...

Upvotes: 10

lawinslow
lawinslow

Reputation: 1001

Yes, the function you're looking for is "getReport". You'll want the 'extended' report.

Using getReport, your code would look like this

        try
             matStart(MatObj);
         catch err
             msgbox('Error in Processing Figures!','Error!','error','modal');
             disp(getReport(err,'extended'));
        end

This will display the same information as an uncaught exception in matlab that prints the full stack trace, though of course the text won't be red.

Upvotes: 23

Related Questions