Try

Revision as of 21:40, 16 November 2017 by Max (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Try(expr, catch, errorNum)

Try() provides an elegant way to respond to possible evaluation errors and skip showing the error message, or show a tailored error message. It also lets you provide code to clean up after a possible error. It tries to evaluate expression «expr». If successful, it simply returns the result from that evaluation. But if it encounters an error while evaluating «expr», or in any User-Defined Function called by «expr», or called by that function and so on, it ceases evaluation, and evaluates the second parameter «catch» if provided. You can add a tailored error message in «catch» and/or code to check on the problem or clean up the result.

It will not catch any errors encountered while evaluating other variables needed to evaluate «expr». But it does catch any error that occurs in a User-Defined Function called directly by «expr», or in a UDF called by a UDF from «expr», and so on.

Also, it does not catch any error that occur in the «catch» expression, unless the Try() is nested within a surrounding Try() function.

Example

This attempts to query a database using the first connection string. If that fails (produces an error), it falls back and tries again with a second connection string:

Try(
DbQuery(connStr1, sql),
Catch:
DbQuery(connStr2, sql)
)

Information about the error

To get information about what error occurred, the «catch» expression can reference three special local variables that contain information about the error:

  • ErrorNumber: A number that identifies which error occurs. This number will be different for different types of errors. When you click on the More Information link in an error message dialog, the URL usually contains this same number.
  • ErrorText: The error message text which you would have seen in an error dialog.
  • ErrorLocation: A handle to the object where the error occurred (either the current variable in the error was is «expr», or a handle to a User-Defined Function, or if this is in an OnClick attribute, it might be a handle to a button.
Try(
DbQuery("DSN = ForrestDB", sql),
Catch:
If ErrorNumber = 43140 Then (
MsgBox("You need to define a data source named 'ForrestDB'. To do so, go to 'Data Sources (ODBC)' on the Start menu and click Add");
Error("Terminated")
) Else (
ReThrow()
)
)

Re-throwing an error

Within the «catch» expression you can call ReThrow() to re-issue the error as if you had never caught it. ReThrow() can only be called from within a «catch» expression.

Filtering by error number

If you only want to catch certain selected errors, you can specify the error number(s) in the «errorNum» parameter. You can specify one or more error numbers.

Try (
DbQuery("DSN = ForrestDB", sql),
Catch:
Error("You need to define a data source named 'ForrestDB'. To do so, go to 'Data Sources (ODBC)' on the Start menu and click Add")
ErrorNum: 43140, 43142
)

If you want to specify a range of numbers use

ErrorNum: ...43000..45000

but beware that this can be expensive if the range is too long (it forms the full sequence internally).

When you want to figure out the error number of a particular error, print it out using a MsgBox and make the error occur like this

Try(expr,
Catch: MsgBox(ErrorNumber)
)

Then once you have the number, you can remove the MsgBox.

Warnings

The Try function has no effect on warnings. To suppress warnings within an expression, use IgnoreWarnings().

History

This function was introduced in Analytica 4.6.

See Also

Comments


You are not allowed to post comments.