Difference between revisions of "Try"

Line 1: Line 1:
 
[[Category:Evaluation Functions]]
 
[[Category:Evaluation Functions]]
''new to [[Analytica 4.6]]''
+
 
  
 
== Try(expr, catch'', errorNum'') ==
 
== Try(expr, catch'', errorNum'') ==
Line 68: Line 68:
  
 
== Warnings ==
 
== Warnings ==
 +
The [[Try]] function has no effect on warnings. To suppress warnings within an expression, use [[IgnoreWarnings]]().
  
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 ==
 
== See Also ==
 
 
* [[IgnoreWarnings]]()
 
* [[IgnoreWarnings]]()
 
* [[Error]]()
 
* [[Error]]()
 
* [[EvaluateScript]](), which also doesn't stop evaluation when an error occurs.
 
* [[EvaluateScript]](), which also doesn't stop evaluation when an error occurs.
 
* [[MsgBox]]()
 
* [[MsgBox]]()

Revision as of 00:41, 23 January 2016


Try(expr, catch, errorNum)

Tries to evaluates expression «expr». If successful, it returns the result. If it encounters an error while evaluating «expr», it ceases evaluation and evaluates «catch» instead. It thus lets your expressions catch an error and continue without displaying an error.

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.