Getting error line number from sys.exc_info

Hi i've seen other threads recommend providing error reporting using code like;
system.gui.errorBox("%s, %s"% (sys.exc_info()[0],sys.exc_info()[1]))
But that doesn't provide offending line number which can be very handy.

For example the following code:

try:
	# some code
except:
	import sys
	logger.error(str(sys.exc_info()))

outputs:

12:58:53.082 [Thread-31] ERROR com.inductiveautomation.factorypmi.application.script.builtin.WindowUtilities - <type 'exceptions.TypeError'>, getValueAt(): 1st arg can't be coerced to int

But if i don't have any try/catch code, i get the following output from the Error popup (compact details):

Traceback (most recent call last):

File "", line 58, in calcQnty

TypeError: getValueAt(): 1st arg can't be coerced to int

Ignition v7.9.9 (b2018081621)
Java: Oracle Corporation 1.8.0_101

Is there anyway to get the line number out when writing exception handling?
Thank very much!

1 Like

I have come up with the following approach for 'catch all' error handling of unexpected errors using this as reference:

import sys
import traceback

logger = system.util.getLogger("ScriptDebugging")

# Some identifier for script name. 'calcQnty' is a custom method on a button component
scriptName = self.name + ".calcQnty"
try:
	# Some code
except:
	exType, ex, tb = sys.exc_info()
	logger.error("ERROR in '%s' line %d: %s, %s" % (scriptName,traceback.tb_lineno(tb), exType, ex))

Provides the following output which i think provides all the info I would need to debug:

13:47:12.268 [Thread-39] ERROR ScriptDebugging - ERROR in 'Button Calc Volume.calcQnty' line 61: <type 'exceptions.TypeError'>, getValueAt(): 1st arg can't be coerced to int

1 Like

You might want to look at my Java wrapper for python exceptions that is part of later.py. You can pass it to a logger to expand into a java-style traceback.

Thanks Phil,
Had a look but i think its a bit over my head to use that wrapper!