Coyote's Guide to IDL Programming

Accurate CATCH Traceback Information

QUESTION: I like to use the Catch error handling mechanism in IDL. But when I try to get error traceback information, the last error line number always points to the line in my Catch error handling code instead of to the line that actually caused the error. How can I make the traceback point to program code line that actually caused the error?

ANSWER: Thanks to Pavel Romashkin (who sensibly ignored my learned advice in this matter and proceeded to find out how things really worked) and the technical support people at RSI, I now know the answer to this question. They pointed out to me that IDL has had the ability to provide this information for quite a while, although I didn't know it and would have been unlikely to discover it myself, I think, from the documentation.

As it happens, the traceback information from the actual error is accessed in IDL by using the Last_Message keyword to the Help command in conjuction with the Output keyword to collect the traceback information.

   Help, /Last_Message, Output=theErrorMessage

Here is a little test program that shows how this is done:

   PRO TEST_ERROR
      ; Error handling code.
   Catch, theError
   IF theError NE 0 THEN BEGIN
      Catch, /Cancel
      Help, /Last_Message, Output=theErrorMessage
      FOR j=0,N_Elements(theErrorMessage)-1 DO BEGIN
         Print, theErrorMessage[j]
      ENDFOR
      RETURN
   ENDIF
      ; The code that causes the error.
   Print, xxx
   END

Running the program results in this output:

   IDL> test
   % PRINT: Variable is undefined: XXX.
   % Execution halted at:  TEST  13 C:\RSI\IDL52\DAVID\test.pro
   %                       $MAIN$                 

This accurate error traceback information has been incorporated into the cgErrorMsg program that implements a device-independent error notification scheme.

[Return to IDL Programming Tips]