Error Handling

<< Click to Display Table of Contents >>

Navigation:  Multi-Protocol MasterOPC Server > Lua 5.1 Reference Manual > The Language and syntax > Error Handling >

Error Handling

If a script error is detected in the run-time mode, an error text is written to the log "Script messages", and further execution of the script is terminated.  

lua_clip0001

If some code of your script can return an error (for instance, a timestamp is incorrectly read out from a device, and this leads to an error while transforming that timestamp), place such a code in a separate function, and invoke that function in the protected mode, using the function pcall .

Example

function StrToTime(str) --transformation of a string to a timestamp

  local str2=string.gsub (str, "%.", "-") --replace a dot by a hyphen in order to correspond to the format

  local timeval=time.StringToTimeStamp(str2..".000"); --transformation to a timestamp

  return timeval;

end;

 

function OnRead ()

NoErr,timeval=pcall(StrToTime,str1); --invocation of transformation function in the protected mode

if NoErr==true then

--there is no an error

else

--there is an error while executing

end

end