Catching Type Conversion Errors

QUESTION: I have a Catch error handler in my program, but it seems that I can't catch a type conversion error. Why not?

Here is a small test program that illustrates the problem. If you set a breakpoint at line 4 and step through it, you see a type conversion error occurs on line 15, but program execution does not jump into the Catch error handler, but instead continues on to the end of the program.

   PRO Test_Catch

      ; Define variable.
      variable = 'coyote'

      ; Set up CATCH error handler.
      Catch, theError
      IF theError NE 0 THEN BEGIN
        Catch, /Cancel
        Print, 'Type Conversion Error Occurred, Repairing...'
        variable = 100.0
      ENDIF

      ; Cause type conversion error.
      newVariable = Float(variable)

      Help, newVariable, variable, theError

   END

The Catch documentation says that "all errors that can be handled by IDL" can be trapped in this way.

ANSWER: Yes, well, the documentation is wrong. [Note: Or, at least it was before the IDL 6.1 version, when they seem to have changed it. Now they advise that "Catch can handle all errors in IDL, except non-fatal math errors." Presumably a type conversion error is one of those.]

I think what the technical writer meant to say was that "all errors that can be handled by IDL, except those that involve file input or output" can be trapped by a Catch error handler. Because a type conversion error is a file input/output error and it definitely cannot be trapped by a Catch error handler. [Note: As of IDL 6.1 (which I just tested), file input and output errors can be trapped with Catch. Type conversion errors still cannot be trapped with Catch, but they can be trapped with On_IOError. Which begs the symantic question, I guess, of what is really meant by an "I/O error" and a "non-fatal math error". I guess you will have to ask RSI.]

File input/output errors are trapped in IDL with the On_IOError command, which allows you to specify a label in your IDL program. When a file input/output error occurs, program execution immediately jumps to the label in the program and program execution continues.

So, for example, to make this short program above work as you would expect it to (and still trap other program errors), you have to modify it a bit. Here is a new version that uses the On_IOError command. Note that On_IOError has a higher order of precidence than Catch, so the two commands can be used together.

   PRO Test_Catch

      ; Define variable.
      variable = 'coyote'

      ; Set up CATCH error handler.
      Catch, theError
      IF theError NE 0 THEN BEGIN
        Catch, /Cancel
        bad_io:
        Print, 'Type Conversion Error Occurred, Repairing...'
        variable = 100.0
      ENDIF

      ; Set up file I/O error handling.
      ON_IOError, bad_io

      ; Cause type conversion error.
      newVariable = Float(variable)

      Help, newVariable, variable, theError

   END

[Return to IDL Programming Tips]