Using Try/Catch/Throw Exceptions
We have used the Try/Catch and Throw actions to display a warning if the geolocation coordinates describe a location outside the USA. These actions are executed when the Try/Catch/Throw button is clicked (see screenshot below).
The Try/Catch/Throw actions
In the design, double-click the Actions dialog icon at the top left of the Try/Catch/Throw button to open the button's Actions dialog (screenshot below).
The actions have been defined as follows:
1.A Try/Catch action was added.
2.We set up a variable $Not-USA-Warning which will be used to hold the exception message.
3.The Try part sets up a condition to test whether the geolocation is not in the USA. This condition is specified in the XPath expression of a Throw action. If the condition is true, then an exception is thrown. The exception message will be stored in the $Not-USA-Warning variable. If the condition is false, then no exception is thrown; we generate the empty sequence so that nothing is stored in the $Not-USA-Warning variable. A description of the XPath expression that does this is given below.
4.The Catch part of the Try/Catch action is processed only if an exception is thrown (that is, only if the condition tested above evaluates to true). In the Catch part, we display a message box that shows the content of the $Not-USA-Warning variable.
XPath expression of the Throw action
The XPath expression of the Throw action is:
if ($MT_GEOLOCATION/Root/Address/@CountryName != 'USA')
then (concat( 'Warning: Device location is outside the US: ', $MT_GEOLOCATION/Root/Address/@CountryName))
else ()
This expression works as follows:
•The if clause checks whether the value of the $MT_GEOLOCATION/Root/Address/@CountryName node is not 'USA'.
•The then clause is processed if the country name is not USA. This clause generates a string.
•The else clause is processed if the country name is USA. It produces an empty sequence
If the geolocation country is not USA, then the condition is true and the expression evaluates to the string generated by the then clause. Since this result is not an empty sequence, an exception is thrown and the generated string is stored in the Try/Catch variable $Not-USA-Warning.
If the geolocation country is USA, then the condition is false and the expression evaluates to an empty sequence (generated by the else clause). Because the result is an empty sequence, no exception is thrown. Therefore, the Catch part of the Try/Catch action is not executed.