Division by Zero in PLC Programs
When debugging a program in KAS, you may run into a situation where there is a division by zero. These errors can be challenging to locate. The solution is to have an exception routine for this condition. Adding a breakpoint in your exception handler will show you the location of the division in the program source code.
Procedure for Writing a Divide By Zero Exception Handler
- Create a new sub-program that will handle the exception. There should be no parameters in the program.
- Edit the Global Defines list to include:
#OnDivZero SubProgramName
Any safety or trace operation can be performed in this subprogram and can choose from the following possibilities:
- Return without any special call. In this case the standard handling will be performed: a system error message is generated, the result of the division is replaced by a maximum value and the application continues.
- Call the FatalStop function. The runtime stops immediately in Fatal Error mode.
- Call the CycleStop function. The runtime finishes the current program and then turns into the "cycle setting" mode.
Handlers can also be used in DEBUG mode for tracking the bad operation. Just put a breakpoint in your handler. When stopped, the call stack will show you the location of the division in the source code of the program. The attached KAS file has a program which will force a division by zero and has an exception handler for this situation.
This procedure and other exception programs are fully detailed in the KAS IDE Online Help. Go to Understanding KAS > IEC 61131-3 > Handling Exceptions.