What Are Exceptions in Ignition and Why Should I Care?
Exceptions are unexpected events that a program cannot take care of on its own, and stop the program from executing. Exceptions can be caused by a wide range of factors, leaving the user wondering why the system isn’t working when improperly managed.
When an exception occurs, the event details are laid out in an exception object, which can be used to better handle the event. By learning to use these details and handle exceptions, developers can write more robust functions, provide useful error messages when things go wrong, and flag conditions that could cause problems downstream.
How to Approach Exception Handling
When handling exceptions, DMC encourages engineers and programmers to stick by these three guidelines:
- Let the code fail but rewrite the error message so that it is more helpful.
- Use the DMC pop-up library to display exceptions as errors to the users.
- Set up the code so that the system handles known and unknown errors consistently.
Handling Exceptions
Exceptions are handled by wrapping error-prone code in a try/except block. There are several types of except statements that can accomplish different things.
Because Ignition uses Jython, a Java implementation of Python, it is also important to handle Java exception types where applicable.
Understanding Python Syntax for Exception Handling
In Python, there are four main keywords for handling exceptions:
- Try: contains error-prone code
- Except: catches and handles errors if they occur
- Else: runs if the try block completes successfully
- Finally: always runs, regardless of what occurs earlier
The core of exception handling occurs in the try and except statements, so we will focus on those in the following examples.
Basic Exception
This example uses the most basic implementation of a try/except statement. You know that an error occurred, but you don’t know anything about the error. Not very useful.
try:
output = dividend / divisor
success = True
except:
success = False
errorMessage = 'An error occurred while dividing'Catch Python Exceptions
You can include the base exception type in the syntax to get the error message or filter for specific error types. It is best practice to look out for specific exception types. In the example below, we catch the ZeroDivisionError type and rewrite the error message so it is more human-readable. Notice how you can include multiple except statements! This way, you can filter for known exceptions and still catch others that may occur during execution of the code.
try:
output = dividend / divisor
except ZeroDivisionError as e:
errorMessage = 'Cannot divide by zero: {}'.format(e.message)
except Exception as e:
errorMessage = 'Division error: {}'.format(e.message)Catch Python and Java Exceptions
Because Ignition uses Jython, a Java implementation of Python, we should also handle Java exception types that might occur during queries, casting, or other scenarios.
import java.lang.Exception as JavaException
try:
output = dividend / divisor
except (Exception, JavaException) as e:
errorMessage = 'Division error: {}'.format(e.message) Displaying and Logging Exceptions
Displaying
DMC’s pop-up library includes functions for automatically displaying exception message pop-ups in Perspective without requiring the declaration of a specific exception type. The pop-ups in this library can display simplified and detailed versions of the error, providing both user-friendly and developer-friendly messages.
import traceback
logger = system.util.getLogger('Test Function')
try:
myFunction(inputA,inputB,inputC)
except Exception as e:
logger.error(traceback.format_exc(e))
Logging
In addition to displaying the exception to users, exceptions should also be logged to the gateway to maintain a record of such events. Without logging, a user can close the exception pop-up, and all information associated with the event will be lost.
Logging with traceback.format_exc(e) creates a well-formatted message with error type, message, and traceback, giving engineers the information they need to debug issues effectively.
from traceback import format_exc
try:
1/0
except Exception as e:
print type(e).__name__
print ''
print e.message
print ''
print format_exc(e)
>>>
ZeroDivisionError
integer division or modulo by zero
Traceback (most recent call last):
File "<input>", line 4, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> Other Exceptions
You can raise your own Exceptions to catch errors that might not violate Python syntax by themselves. This is useful for sanitizing inputs or generally checking statuses before they cause errors later in program execution. You can also reraise within an Except block to add more context to an error message.
def divideNumbers(dividend, divisor):
if divisor == 0:
raise ValueError('Cannot divide by 0')
try:
output = dividend / divisor
except Exception as e:
raise Exception('Division error: {}'.format(e.message))
return outputSQL Exceptions
When considering SQL exceptions, we first want to examine the query method. Where possible, we want to use named queries instead of prep queries because named queries provide much better error messages.
Custom Exception Classes
Creating custom exception classes is incredibly easy to accomplish in Python. You might want to create separate exception types for user input errors and system errors.
Conclusion
By moving beyond basic try/except usage and adopting a more structured approach, like catching specific exception types, handling both Python and Java errors in Ignition, and logging details, you can turn unexpected errors into actionable insights.
At DMC, we always apply these best practices across our automation and software projects to deliver systems that are robust, user-friendly, and scalable. Whether you’re building a new Ignition application or improving existing systems, stronger exception handling helps ensure greater dependability and performance.
Kicking off an automation project? DMC can help you take the next step!
Discover more about Ignition solutions and HMI Programming services with DMC and contact an expert today.







