One aspect of shortcuts that can be frustrating is error handling. Often, when an error occurs, the shortcut simply reports an error and stops. In the following example, the List action contains two items and the Get Item from List action attempts to get item three:
The Shortcuts app does not have anything similar to AppleScript’s try statement. So, when possible, the best approach is to prevent the error from happening:
Another alternative is to place the actions that may cause an error in a separate shortcut and to call that shortcut by way of an AppleScript with a try statement. There’s many ways this could be implemented and the following is a simple one:
There are two additional options. One is to use JavaScript to catch the error–I’m not familiar with JavaScript and can’t give an example of this. The second is to use a x-callback-url (documentation here). This seems useful, but, unfortunately, it loads the Shortcuts app whenever it is used.
The Shortcuts app’s documentation (here) contains the following:
Occasionally, a running shortcut may encounter an error or other issue. For example, an action that grabs the current Wi-Fi network name may grab a network that’s not your home network. If you expect that an action may create an issue, you can use the Stop Shortcut action or the Stop and Output action inside of an If action to halt the shortcut if a specific condition occurs. Stop actions are similar to a return or break statement in Swift.
This sounds great, but it doesn’t appear to work in some circumstances. Using my original example:
The shortcuts from above:
List.shortcut (21.7 KB)
List 1.shortcut (22.1 KB)
List 2.shortcut (22.1 KB)
List 3.shortcut (21.9 KB)