Difference between revisions of "Try"

(category)
(flags and finalizer)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[Category:Evaluation Functions]]
 
[[Category:Evaluation Functions]]
''new to [[Analytica 4.6]]''
 
  
= Try( expr, catch'', errorNum'' ) =
+
{{ReleaseBar}}
  
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.
+
== Try(expr, catch'', errorNum{{Release|6.5||, flags, finalizer}}'') ==
  
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.
+
<code>[[Try]]()</code> 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.
  
Errors that occur in the «catch» expression are not caught, but will be caught by a surrounding [[Try]]() if it is nested.
+
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.
  
= Example =
+
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:
 
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]](
+
:<code>Try(</code>
    [[DbQuery]](connStr1,sql),
+
::<code>DbQuery(connStr1, sql),</code>
Catch:
+
:<code>Catch:</code>
    [[DbQuery]](connStr2,sql)
+
::<code>DbQuery(connStr2, sql)</code>
)
+
:<code>)</code>
 
 
 
 
= Information about the error =
 
  
 +
== 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 26:
 
* '''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.
  
[[Try]](
+
:<code>Try(</code>
    [[DbQuery]]("DSN=ForrestDB",sql),
+
::<code>DbQuery("DSN = ForrestDB", sql),</code>
Catch:
+
:<code>Catch:</code>
    [[If]] ErrorNumber=43140 [[Then]] (
+
::<code>If ErrorNumber = 43140 Then (</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>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>
        [[Error]]("Terminated")
+
:::<code>Error("Terminated")</code>
    ) [[Else]] (
+
::<code>) Else (</code>
        [[ReThrow]]()
+
:::<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.
  
[[Try]] (
+
:<code>Try (</code>
      [[DbQuery]]("DSN=ForrestDB",sql),
+
::<code>DbQuery("DSN = ForrestDB", sql),</code>
Catch:
+
:<code>Catch:</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>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>
ErrorNum: 43140, 43142
+
:<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
  
ErrorNum: ...43000..45000
+
:<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 62:
 
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
  
[[Try]]( expr,
+
:<code>Try(expr,</code>
Catch: [[MsgBox]](ErrorNumber)
+
:<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]].
 +
 +
=={{Release|6.5|| Catching errors globally }}==
 +
{{Release|6.5||
 +
''New in [[Analytica 6.5]]''
 +
Try catches evaluation errors that occur directly within «expr» or within functions called from «expr», but it does not catch syntax errors or evaluation errors that occur elsewhere in the model. During the evaluation of «expr», the value from other global variables may be required, causing them to evaluate and encounter errors.
 +
 +
If you want to catch all errors that occur while «expr» is being evaluated, even if they occur elsewhere in the model, then you can specify the «flags» parameter.
 +
:<code>[[Try]]( expr, flags:3, catch... )</code>
 +
}}
 +
The <code>flags:1</code> bit catches evaluation errors globally, and <code>flags:2</code> catches parsing errors globally, so their sum, <code>flags:3</code> catches all errors, ensuring that no error will be reported during the evaluation. Although you can specify 1 or 2, only the value of 3 is reliably supported.
 +
 +
A user abort (e.g., Ctrl+Break) is not caught even with the flags set.
 +
 +
=={{Release|6.5|| Finalizer }}==
 +
{{Release|6.5||
 +
An optional finalizer expression is evaluated after «expr» and «catch» in all situations, even in the event of a user abort (e.g., Ctrl+Break). This can be useful when you need to ensure that clean up code runs, even in the case where the calculation in aborted.
 +
[[Local]] tmpFilename :{{=}} ...;
 +
[[Try]] (
 +
    [[WriteTextFile]](tmpFilename, ...);
 +
    ...
 +
  , catch: [[ReThrow]]()
 +
  , finalizer: [[FileSystemDelete]](filename) )
 +
}}
 +
 +
== 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]].
 +
 +
«flags» and «finalizer» parameters were introduced in [[Analytica 6.5]].
 +
 +
== See Also ==
 +
* [[IgnoreWarnings]]()
 +
* [[Error]]()
 +
* [[EvaluateScript]](), which also doesn't stop evaluation when an error occurs.
 +
* [[MsgBox]]()

Latest revision as of 23:28, 23 July 2024




Release:

4.6  •  5.0  •  5.1  •  5.2  •  5.3  •  5.4  •  6.0  •  6.1  •  6.2  •  6.3  •  6.4  •  6.5


Try(expr, catch, errorNum, flags, finalizer)

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.

Catching errors globally

New in Analytica 6.5 Try catches evaluation errors that occur directly within «expr» or within functions called from «expr», but it does not catch syntax errors or evaluation errors that occur elsewhere in the model. During the evaluation of «expr», the value from other global variables may be required, causing them to evaluate and encounter errors.

If you want to catch all errors that occur while «expr» is being evaluated, even if they occur elsewhere in the model, then you can specify the «flags» parameter.

Try( expr, flags:3, catch... )

The flags:1 bit catches evaluation errors globally, and flags:2 catches parsing errors globally, so their sum, flags:3 catches all errors, ensuring that no error will be reported during the evaluation. Although you can specify 1 or 2, only the value of 3 is reliably supported.

A user abort (e.g., Ctrl+Break) is not caught even with the flags set.

Finalizer

An optional finalizer expression is evaluated after «expr» and «catch» in all situations, even in the event of a user abort (e.g., Ctrl+Break). This can be useful when you need to ensure that clean up code runs, even in the case where the calculation in aborted.

Local tmpFilename := ...;
Try (
    WriteTextFile(tmpFilename, ...);
    ...
 , catch: ReThrow()
 , finalizer: FileSystemDelete(filename) )

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.

«flags» and «finalizer» parameters were introduced in Analytica 6.5.

See Also

Comments


You are not allowed to post comments.