Single-Pass Loop

This article explains the technique known as a single-pass loop, and why it is part of my script template. If you do a lot of error trapping or exception handling, this is a great technique to master.

The single-pass loop technique is inspired by the Try-Catch statement found in many programming languages. In a Try-Catch statement, the developer will put the happy path logic in the Try block, and if an exception happens, the code will jump into the Catch block to deal with the exception.

Example Try-Catch Statement using JavaScript

The single-pass loop technique achieves this in FileMaker by using a loop. If everything goes well, the script will go down the list of script steps until it reaches the end of the loop, then jump out of the loop. The loop will cycle through one time at most, which is how this technique gets its name.

Then if something goes wrong and is captured by these Exit Loop If script steps, the script will jump out of the loop without executing the rest, and jump into the Catch block to handle this error.

Try-Catch Statement in FileMaker

Single-Pass Loop vs. Exception Handling

So what’s the advantage of using a single pass loop over other exception handling methods?

Here’s a comparison between the same script written in both methods.

With a single-pass loop
Same logic, without a single-pass loop

In the two scripts above, I’ve put a pin next to each script step that contributes to error handling. The one with a single-pass loop only has one script step per paragraph to capture the error and a centralized handling logic at the end. Whereas the one without a single-pass loop uses six script steps per paragraph to capture and handle errors.

Loop Advantages

The former reads much better than the latter as the error handling script steps don’t distract the reader from the actual business logic the script is trying to do. This makes the aforementioned loop a simple and elegant method to handle exceptions. It adds very little overhead on the developer, which will make it easier to adopt.

For further reading on this method, this is a great resource. You can find out more about error handling in FileMaker here, or check out our video on this technique.