MobileTogether 2.1 includes new Try/Throw and Catch error handling actions for mobile apps, allowing developers to build sophisticated error-handling routines that improve end-user experience. For instance, if an app wants to connect to a third-party Web service but the server is not available, error-handling actions permit graceful recovery.
MobileTogether error handling actions for mobile apps work the same way for all platforms, saving developers time since there’s no need to customize error handling based on the requirements of each mobile operating system.
Our earlier post titled REST Services as Data Sources for Mobile Apps used a demo app that calls a REST service hosted by USGS to get accurate elevation values for geolocation coordinates.
We can use Try/Catch to manage two different errors that might occur based on execution of the REST service:
- We might encounter a connection error to the USGS server
- We could receive an elevation value of -1000000, which is the USGS telling us it has no elevation data for the supplied coordinates
We can place the entire REST service call and subsequent actions to manipulate data inside Try, as shown here:
If connection to the Web service fails, all subsequent actions inside the Try action are aborted. If we don’t get any data from the USGS, there is no point in attempting the Update Node(s) action highlighted above. In this example a single action would be aborted, but you can place multiple actions, action groups, or even action groups that call other action groups inside the Try action.
Similarly, the Catch action can also perform multiple operations that are only executed when the error occurs. In the screenshot above, Catch displays a message box indicating the connection failed and it performs Update Node(s) using the same elevation value USGS would provide for an out of range error. Of course you can place multiple actions, action groups, or even action groups that call other action groups inside the Catch action just like Try.
More Complex Flow Control with Error Handling Actions for Mobile Apps
The true power of try/catch/throw error handling starts to come into play when a mobile app requires more complex program logic.
Consider the case where one action group contains a series of operations – such as database commands or web services calls – that need to all be executed in sequence, and where each call depends on the success of the previous one. It quickly becomes way too cumbersome to check for the result of each step in an if-else statement. Instead, you can cleanly and elegantly define database action to trigger a throw if there is an error, and then have one clean catch at the bottom where the error is displayed.
This screenshot shows try/catch in the Altova Company Financials mobile application that lets you explore and analyze US public company high-level financial reports automatically extracted from the XBRL filings in the SEC’s EDGAR database:
If any of the five database queries fails, an error is thrown, the rest of the actions under Try are skipped, and the variable holding the error message text reports which query failed.
Nested Try/Catch Error Handling
You can even define a try/catch error handler in one action group and let the throw happen in a separate sub- action group that is called from the main action. In fact, this could be nested deeply and the error could happen somewhere in an action nested several times.
At that point, control passes directly back to the outer error handler’s catch branch where the error is processed, exactly the way try/catch/throw works in other programming languages.
The MobileTogether Designer with try/catch/throw error handling actions for mobile apps is free – download your copy now!