system.util.jsonDecode error not caught by try/except in gateway script

When I get an error in the system.util.jsonDecode function running in a global script the error appears in the diagnostic log on the web interface (http://localhost:8088/main/web/status/diag.logviewer?5) and the script aborts. I would expect the except portion of the code to run and the script to continue. I am calling my script every two seconds in a Gateway event script

badJSONstringExample = "{"
try:
   steeringPlanDict = system.util.jsonDecode(badJSONstringExample )
except Exception, e:
   #never gets here
   isValid = False
   eventMessage = "Failed to decode JSON string"
   logError (eventMessage)

It’s probably a Java exception and you’re trapping Python exceptions with your except statement. Unfortunately they don’t inherit from the same class.

Try adding
except java.lang.Exception as e:

Great tip ! Thanks

Here is the working code… note slightly different except syntax:

badJSONstringExample = "{"
try:
   steeringPlanDict = system.util.jsonDecode(badJSONstringExample )

#except Exception, e:                           #this doesn't work
except java.lang.Exception ,  e:            #this does
   #now it gets here
   isValid = False
   eventMessage = "Failed to decode JSON string: " + e.toString()
   logError (eventMessage)
1 Like