How to properly use GetRTError?

Dear igoristas,

yesterday I fixed a nasty bug regarding stale run time errors (RTE).

Consider the following example:

#pragma rtFunctionErrors=1

Function DoStuff()

    DebuggerOptions enable=0

    variable a = str2num("a")

    a = str2num("123")

    print GetRTErrMessage()
    variable err = GetRTError(1)
    print err
End


which gives

•dostuff()
  Str2num;expected number
  1001


Due to "rtFunctionErrors" the first str2num generates an RTE. The next str2num suceeds, but not clears the RTE.
The RTE seems to not be cleared by a successfull operations.
This greatly "uglyfies" error handling as I have to do:

// clear
err = GetRTError(1)
// call operation
a = str2num("str")
if (GetRTError(1))
// handle error
endif


My example is contrived but if error generation and error checking have 10 function calls depth in between it is not so obvious.

Is the ugly approach really the only correct one?
Yes, you understand correctly. GetRTError works similarly to the Windows API GetLastError function (https://msdn.microsoft.com/en-us/library/windows/desktop/ms679360(v=vs…). If you want to know whether a specific call to an Igor function or operation produces a run time error, you'd want to do something like this:
Variable dummy = GetRTError(1)  // Clears any existing error code
[call some Igor function or operation]
dummy = GetRTError(1)  // Clears any existing error code
if (dummy != 0)
  // handle error here
endif


You can also use the AbortOnRTE keyword within a try...catch block. For example:
#pragma rtFunctionErrors=1

Function DoStuff()
 
    try
        variable a = str2num("a"); AbortOnRTE
     
        a = str2num("123"); AbortOnRTE
    catch
        print GetRTErrMessage()
        variable err = GetRTError(1)
        print err
    endtry
       
End