#DEFINE _NL_ CHR(13)+CHR(10) * Let's first call PSub2 directly DO PSub2 * Now call it indirectly within context where ERROR() event is present. oTst = CREATEOBJECT("T_ErrTrap") oTst.DoCmd("DO PSub2") IF oTst.bOkFlg THEN ? "OK" ELSE ? oTst.cErrMsg ENDIF RELEASE oTst RETURN .T. PROCEDURE PSub1 LOCAL cErrTrap, nErrNum cErrTrap = ON("ERROR") ON ERROR nErrNum = ERROR() nErrNum = 0 = "A"+1 ON ERROR &cErrTrap. ? "nErrNum = ", nErrNum ENDPROC PROCEDURE PSub2 DO PSub1 ENDPROC DEFINE CLASS T_ErrTrap AS CUSTOM bOKFlg = .T. && Indicates if everything ran OK or not cErrMsg = "" && Error message indicating nature of failure PROCEDURE ERROR LPARAMETERS nError, cMethod, nLine THIS.bOkFlg = .F. THIS.cErrMsg = "Error "+TRANSFORM(m.nError) ; +":"+MESSAGE()+_NL_ ; +"Error on line "+TRANSFORM(m.nLine)+" in "+m.cMethod+_NL_ ; +"Command: "+MESSAGE(1) RETURN ENDPROC PROCEDURE DoCmd( cCmd ) &cCmd. ENDPROC ENDDEFINE && T_ErrTrap>In some legacy code there's a function SafeCmd() to execute some commandline and do this safely. If it fails it returns an error number. It always worked well, until recently. The command "use xxxx exclusive alias xxxx" correctly returns error 1705 (access denied) if the table is already opened by some other application. However, in another routine a similar call incorrectly returns error 0.
FUNCTION SafeCmd > LPARAMETER m.Command > > local m.Error, m.OnError >* private m.Error, m.OnError > > m.OnError = ON( 'ERROR' ) > m.Error = 0 > > ON ERROR m.Error = ERROR() > &Command > ON ERROR &OnError > > RETURN m.Error