Try catch on system.tag.write

Im using python and writing to a tag, if the tag fails to write, i get a red error box on the screen. I want to catch the error but the generic catch wont stop the error. Does anyone know the error type i need to catch?

Here is the error:

ERROR [ClientTagUtilities-AWT-EventQueue-2] Error writing to tag RFID/string_input_ignition_uid_get Bad Quality


You could try instead to follow the write with a read and then look at the intValue of the Quality Overlay:

system.tag.write("MyTag", value)
qv ="MyTag")
print qv.quality.intValue
1 Like

Here are a couple ideas:

The system.tag.write function has a third optional parameter called “suppressErrors”. Try setting that to True. For example: system.tag.write(tagPath, value, True)

If you actually want to catch an exception when writing to a tag then you could try using system.tag.writeSynchronous.

The reason you can’t catch an exception when using system.tag.write is because it is executed in a separate thread than your script that calls system.tag.write. Because of this system.tag.write is considered an asynchronous function.

The function system.tag.writeSynchronous is not asynchronous. It is executed in the same thread that it is called in and it is executed directly after it is called.

You could use the following function to still achieve asynchronous tag writes but be able to catch exceptions when writing to a tag fails:

def tagWrite(tagPath,value): import sys,system def func(): try: system.tag.writeSynchronous(tagPath, value) except: #get and print out information about the exception that was raised print sys.exc_info() system.util.invokeAsynchronous(func)

1 Like

Thanks, I ended up using the system.opc.write and used the isGood() to let me know if the write failed or not.

I appreciate the background on how the tags work!

1 Like