Difference between revisions of "Try"
Line 2: | Line 2: | ||
''new to [[Analytica 4.6]]'' | ''new to [[Analytica 4.6]]'' | ||
− | = Try( expr, catch'', errorNum'' ) = | + | == 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. | 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. | + | 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. | Also, it does not catch any error that occur in the «catch» expression, unless the [[Try]]() is nested within a surrounding [[Try]]() function. | ||
− | = Example = | + | == 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: | 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: | ||
− | + | :<code>Try(</code> | |
− | + | ::<code>DbQuery(connStr1, sql),</code> | |
− | + | :<code>Catch:</code> | |
− | + | ::<code>DbQuery(connStr2, sql)</code> | |
− | + | :<code>)</code> | |
− | |||
− | |||
− | |||
+ | == 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: | 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. | * '''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. | ||
Line 28: | Line 25: | ||
* '''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. | * '''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. | ||
− | + | :<code>Try(</code> | |
− | + | ::<code>DbQuery("DSN = ForrestDB", sql),</code> | |
− | + | :<code>Catch:</code> | |
− | + | ::<code>If ErrorNumber = 43140 Then (</code> | |
− | + | :::<code>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");</code> | |
− | + | :::<code>Error("Terminated")</code> | |
− | + | ::<code>) Else (</code> | |
− | + | :::<code>ReThrow()</code> | |
− | + | ::<code>)</code> | |
− | + | :<code>)</code> | |
<div id="ReThrow"> | <div id="ReThrow"> | ||
− | = Re-throwing an error = | + | == Re-throwing an error == |
</div> | </div> | ||
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. | 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 = | + | == 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. | 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. | ||
− | + | :<code>Try (</code> | |
− | + | ::<code>DbQuery("DSN = ForrestDB", sql),</code> | |
− | + | :<code>Catch:</code> | |
− | + | ::<code>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")</code> | |
− | + | :<code>ErrorNum: 43140, 43142</code> | |
− | + | :<code>)</code> | |
If you want to specify a range of numbers use | If you want to specify a range of numbers use | ||
− | + | :<code>ErrorNum: ...43000..45000</code> | |
but beware that this can be expensive if the range is too long (it forms the full sequence internally). | but beware that this can be expensive if the range is too long (it forms the full sequence internally). | ||
Line 64: | Line 61: | ||
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 | 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 | ||
− | + | :<code>Try(expr,</code> | |
− | + | :<code>Catch: MsgBox(ErrorNumber)</code> | |
− | + | :<code>)</code> | |
Then once you have the number, you can remove the [[MsgBox]]. | Then once you have the number, you can remove the [[MsgBox]]. | ||
− | = 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]](). | ||
− | = See Also = | + | == See Also == |
* [[IgnoreWarnings]]() | * [[IgnoreWarnings]]() |
Revision as of 20:05, 12 January 2016
new to Analytica 4.6
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().
See Also
- IgnoreWarnings()
- Error()
- EvaluateScript(), which also doesn't stop evaluation when an error occurs.
- MsgBox()
Enable comment auto-refresher