Error handling is critical in any software development. When done correctly and with the user in mind, error handling makes it easier to operate a system correctly, lets users know when something has gone wrong (and ideally how to fix it), and makes your code easier to debug and maintain. In this blog, I’ll discuss the common error handling techniques for Ignition SCADA applications and key considerations to keep in mind to provide the best possible user experience.
The goal is to identify and eliminate the potential for errors before they actually become errors and, failing that, to handle them in a way that does not break your application. First, anticipate and account for every possible thing that a user can do, any variation of information or format they could enter into a field, any illegal function they may perform, and any other possible way they may accidentally break the code and cause the system to stop working correctly.
Once you’ve accounted for any and all possibilities, provide helpful alerts and responses for each possible error that lets a user know exactly what went wrong, so they know how to fix the problem. Finally, do something with the errors: log the error so it’s easy to find during debugging or compensate for the error if it’s something you can fix in your code to allow the user to keep going without disruption.
To catch errors in Python, you can use the “try” and “except” keywords. For a more organized script, the “else” and “finally” keywords can be added.
The Try and Except blocks within Python are a common way to handle errors in situations that require user input or external communications. The Try block is where you run the code that you think has the potential to cause an error, such as a user spelling out a number instead of using numerals in an input field or querying a value from a database with an unstable connection. The Except blocks are where specific and unspecific errors are caught and handled. For instance, it can give the user a specific error message, letting them know their attempt failed, or correct the issue internally and continue running the system with no indication that anything happened.
A common sight in Ignition applications is the following:
This example should not be considered error handling for two reasons. First, there is only one except statement meaning that all possible errors will be handled in the exact same way. Second, the pass statement tells the system to ignore the error entirely and continue running. There will be no log of the error ever occurring with the only indication that something happened is that whatever was supposed to happen did not.
Instead, error handling should anticipate the errors that are likely to occur, compensate for them or provide a useful message when something goes wrong that can not be fixed. In the example below, you’ll see different code blocks are used for specific exceptions with unique messages to inform the user or developer.
If no errors were encountered in the Try block, the Else block comes into play. This block will give further instruction and keep code organized and clear if no exceptions were found from the Try block. In the example below, the Else block is performing the calculation to turn the value into a percentage. At this point we know that c is going to be a valid number and therefore this operation is going to succeed and does not need to belong in another try block. This is a simple example and in reality, the else block could contain several more lines of more complex code.
The Finally block is code that always happens at the end of an execution. For example, if you want to log what happens in your code regardless of specific circumstances, you would use the Finally block to do so. In this example, we’re just going to return the value and a status message.
These are a few of the error handling techniques in Ignition SCADA software that will help ensure a good user experience and seamless execution for SCADA systems created in this software.