Difference between revisions of "Try"
(category) |
|||
Line 69: | Line 69: | ||
Then once you have the number, you can remove the [[MsgBox]]. | 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]]() |
Revision as of 22:24, 18 March 2014
new to Analytica 4.6
Try( expr, catch, errorNum )
Attempts to evaluates «expr» and returns its result. If an error occurs during the evaluation of «expr», the remaining evaluation of «expr» is aborted and «catch» is evaluated instead. This allows your expressions to catch an error and continue without displaying the error to the user.
Errors that occur in other variables are not caught, even if those variables are evaluated in order to compute the result for «expr». An errors that occur in User-Defined Functions called directly by «expr» (or in a UDF called by a UDF called from a UDF called from «expr», etc.) is caught.
Errors that occur in the «catch» expression are not caught, but will be caught by a surrounding Try() if it is nested.
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