System.net.sendmail not getting trapped by try except

I have the following code and when I get an error it does not do the except, it crashes and dumps the error message into the wrapper log file. In this case it was an invalid email address, I am now validating every email address, but if I miss something in the error checking, or a server error occurs, or anything else, the script crashes and no further processing occurs and the user has no idea that the email was not sent (doesn't execute WriteToQCAuditFile).

Is there any way to trap ALL errors with this command?

	try:
		system.net.sendEmail(smtp="10.10.101.2:25", fromAddr="Info@domain.com", subject=Subject, body=Message,
		                     to=recipients, attachmentNames=[pdfFilename], attachmentData=[pdfData])
	except Exception as e:
		if debug >= 0:  print("SendPDFEmail - Error sending email:", str(e))
		WriteToQCAuditFile("N/A", pdfFilename, "Error sending email - email not sent")
		return 'Error'

Error log:
INFO   | jvm 1    | 2025/05/22 18:50:17 | com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
INFO   | jvm 1    | 2025/05/22 18:50:17 |   File "<TimerScript:BrixSampler/SendQCEmailsOrg @5,000ms >", line 53, in <module>
INFO   | jvm 1    | 2025/05/22 18:50:17 |   File "<module:project.QCEmail>", line 33, in SendPDFEmailIgn
INFO   | jvm 1    | 2025/05/22 18:50:17 |   File "<module:project.QCEmail>", line 33, in SendPDFEmailIgn
INFO   | jvm 1    | 2025/05/22 18:50:17 | 	at javax.mail.internet.InternetAddress.<init>(InternetAddress.java:122)
INFO   | jvm 1    | 2025/05/22 18:50:17 | 	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities.sendEmail(GatewayNetUtilities.java:228)
INFO   | jvm 1    | 2025/05/22 18:50:17 | 	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities.sendEmail(GatewayNetUtilities.java:130)
INFO   | jvm 1    | 2025/05/22 18:50:17 | 	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities._sendEmail(GatewayNetUtilities.java:57)
INFO   | jvm 1    | 2025/05/22 18:50:17 | 	at com.inductiveautomation.ignition.common.script.builtin.AbstractNetUtilities.sendEmail(AbstractNetUtilities.java:170)
INFO   | jvm 1    | 2025/05/22 18:50:17 | 	at jdk.internal.reflect.GeneratedMethodAccessor4112.invoke(Unknown Source)
INFO   | jvm 1    | 2025/05/22 18:50:17 | 	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
INFO   | jvm 1    | 2025/05/22 18:50:17 | 	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
INFO   | jvm 1    | 2025/05/22 18:50:17 | javax.mail.internet.AddressException: javax.mail.internet.AddressException: Illegal address in string ``dxxxx@mxxxxx.com, vigxxxxx@xxxxx.com''

This should be a list, not a string.

recipients = ["dxxxx@mxxxxx.com", "vigxxxxx@xxxxx.com"]

except BaseException instead of Exception

should get everything

It gets converted to a list before the email is sent. In this case, they typed the email wrong with a comma delimiter instead of a semicolon delimiter. I am error checking the email addresses now and allowing pretty much any reasonable format. But that is not the point, if there is some other unhandled exception that I didn't consider or the email server fails or whatever happens, you can see that the try/except did not always work. It didn't work with even something as simple as an invalid email address. It crashed (see output from wrapper log), the except clause did not get executed and the script stopped processing.

Is there a way to capture all possible errors that can occur in the system.net.sendEmail command?

Sorry, I didn't see this before I rambled for so long on the previous reply. I will try that. Thank you!

It still crashed on the sendEmail command (line 52). I setup an invalid email string and commented out the error checking to allow to try the sendEmail command. Strange that it doesn't catch the error. Any ideas on what I am doing wrong?

Here is the code:

#
#  send an email with a pdf attachment
#
def SendPDFEmailIgn(PDFPathAndFilename, Subject, Message, EmailList):
	debug = 0
	test = 1

	#  test - send all emails to mikez@compuzak.com and mikez@pf-pv.com only
	if test == 1:  EmailList = 'xxxx@yyyyyy.com, xxxxxxx@ymail.com'

    #  prepare semicolon delimited email list for mail command
	recipients = [email.strip() for email in EmailList.split(";") if email.strip()]
	if debug >= 5: print "SendPDFEmailIgn - Recipients:", recipients

	#  make sure there are recipients
	if not recipients:
		if debug >= 0: print("SendPDFEmailIgn - No valid recipients found.")
		WriteToQCAuditFile("N/A", PDFFilename, "Error no email recipients - email not sent")
		return 'Error'

	#  set attachment data
	try:
		pdfFilename = PDFPathAndFilename.split("\\")[-1]
		pdfData = system.file.readFileAsBytes(PDFPathAndFilename)
		if debug >= 5: print "SendPDFEmailIgn - pdfFileName:", pdfFilename + ' size of pdf data is ' + str(len(pdfData))
	except Exception as e:
		if debug >= 0:  print("SendPDFEmail - error processing pdf file:", str(e))
		WriteToQCAuditFile("N/A", pdfFilename, "Error processing pdf file - email not sent")
		return 'Error'	

#	#  error check each email address
#	errMessage = ''
#	for email in recipients:
#		if email.count(',') > 0:
#			errMessage = 'Error - email address contains a comma'
#			break
#		elif email.count('@') > 1:
#			errMessage = 'Error - email address has multiple @ signs'
#			break
#		elif email.count('@') == 0:
#			errMessage = 'Error - email address missing @ sign'
#			break
#		elif email.count('.') == 0:
#			errMessage = 'Error - email address missing . in domain name'
#			break
#	if len(errMessage) > 0:
#		project.QCEmail.WriteToQCAuditFile("N/A", pdfFilename, errMessage)
#		return 'Error'

	#  send email message
	try:
		system.net.sendEmail(smtp="10.10.101.2:25", fromAddr="Info@xxxxx.com", subject=Subject, body=Message,
		                     to=recipients, attachmentNames=[pdfFilename], attachmentData=[pdfData])
	except BaseException as e:
		if debug >= 0:  print("SendPDFEmail - Error sending email:", str(e))
		WriteToQCAuditFile("N/A", pdfFilename, "Error sending email - email not sent")
		return 'Error'

	#  completion message
	if debug >= 5: print "SendPDFEmailIgn - completed successfully"
	return "Success"

Here is the error from the wrapper log:

INFO   | jvm 1    | 2025/05/22 22:26:53 | E [c.i.i.c.s.TimerScriptTask     ] [05:26:53]: Error executing global timer script: BrixSampler/SendQCEmailsOrg @5,000ms . Repeat errors of this type will be logged as 'debug' messages. 
INFO   | jvm 1    | 2025/05/22 22:26:53 | com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
INFO   | jvm 1    | 2025/05/22 22:26:53 |   File "<TimerScript:BrixSampler/SendQCEmailsOrg @5,000ms >", line 53, in <module>
INFO   | jvm 1    | 2025/05/22 22:26:53 |   File "<module:project.QCEmail>", line 52, in SendPDFEmailIgn
INFO   | jvm 1    | 2025/05/22 22:26:53 |   File "<module:project.QCEmail>", line 52, in SendPDFEmailIgn
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at javax.mail.internet.InternetAddress.<init>(InternetAddress.java:122)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities.sendEmail(GatewayNetUtilities.java:228)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities.sendEmail(GatewayNetUtilities.java:130)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities._sendEmail(GatewayNetUtilities.java:57)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.common.script.builtin.AbstractNetUtilities.sendEmail(AbstractNetUtilities.java:170)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at jdk.internal.reflect.GeneratedMethodAccessor4112.invoke(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | javax.mail.internet.AddressException: javax.mail.internet.AddressException: Illegal address in string ``xxxx@yyyyyy.com, xxxxxxx@ymail.com''
INFO   | jvm 1    | 2025/05/22 22:26:53 | 
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.core.Py.JavaError(Py.java:547)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities._sendEmail(GatewayNetUtilities.java:61)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.common.script.builtin.AbstractNetUtilities.sendEmail(AbstractNetUtilities.java:170)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at jdk.internal.reflect.GeneratedMethodAccessor4112.invoke(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.common.script.ScriptManager$ReflectedInstanceFunction.__call__(ScriptManager.java:552)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.core.PyObject.__call__(PyObject.java:400)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.pycode._pyx81730.SendPDFEmailIgn$1(<module:project.QCEmail>:61)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.pycode._pyx81730.call_function(<module:project.QCEmail>)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.core.PyTableCode.call(PyTableCode.java:173)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.core.PyBaseCode.call(PyBaseCode.java:187)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.core.PyFunction.__call__(PyFunction.java:449)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.pycode._pyx81729.f$0(<TimerScript:BrixSampler/SendQCEmailsOrg @5,000ms >:64)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.pycode._pyx81729.call_function(<TimerScript:BrixSampler/SendQCEmailsOrg @5,000ms >)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.core.PyTableCode.call(PyTableCode.java:173)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.core.PyCode.call(PyCode.java:18)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at org.python.core.Py.runCode(Py.java:1687)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:803)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:823)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:751)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:804)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.common.script.TimerScriptTask.run(TimerScriptTask.java:90)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at java.base/java.util.concurrent.FutureTask.runAndReset(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at java.base/java.lang.Thread.run(Unknown Source)
INFO   | jvm 1    | 2025/05/22 22:26:53 | Caused by: org.python.core.PyException: javax.mail.internet.AddressException: javax.mail.internet.AddressException: Illegal address in string ``xxxx@yyyyyy.com, xxxxxxx@ymail.com''
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	... 30 common frames omitted
INFO   | jvm 1    | 2025/05/22 22:26:53 | Caused by: javax.mail.internet.AddressException: Illegal address
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at javax.mail.internet.InternetAddress.<init>(InternetAddress.java:122)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities.sendEmail(GatewayNetUtilities.java:228)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities.sendEmail(GatewayNetUtilities.java:130)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities._sendEmail(GatewayNetUtilities.java:57)
INFO   | jvm 1    | 2025/05/22 22:26:53 | 	... 28 common frames omitted


from java.lang import Throwable

try:
    something
except Exception:
    logger.error("Caught a python error")
except Throwable:
    logger.error("Caught a java error")
8 Likes

This is the way.

It does not. Java and Python exceptions do not have a common base class. Pascal's two-clause example is required to catch both java and jython exceptions.

4 Likes

Thank you pascal, worked great!

TIL, and TY!