HostFunctionstryCatch Method |
Allows script code to handle host exceptions.
Namespace:
Microsoft.ClearScript
Assembly:
ClearScript.Core (in ClearScript.Core.dll) Version: 7.3.1
Syntaxpublic bool tryCatch(
Object tryFunc,
Object catchFunc,
Object finallyFunc = null
)
Public Function tryCatch (
tryFunc As Object,
catchFunc As Object,
Optional finallyFunc As Object = Nothing
) As Boolean
public:
bool tryCatch(
Object^ tryFunc,
Object^ catchFunc,
Object^ finallyFunc = nullptr
)
member tryCatch :
tryFunc : Object *
catchFunc : Object *
?finallyFunc : Object
(* Defaults:
let _finallyFunc = defaultArg finallyFunc null
*)
-> bool
Parameters
- tryFunc
- Type: SystemObject
A script function that invokes one or more host methods or properties. - catchFunc
- Type: SystemObject
A script function to invoke if tryFunc throws an exception. - finallyFunc (Optional)
- Type: SystemObject
An optional script function that performs cleanup for the operation.
Return Value
Type:
BooleanTrue if
tryFunc completed successfully,
false if it threw an exception that was handled by
catchFunc.
Remarks
This function uses a try-catch-finally statement to invoke
tryFunc. If an exception is thrown, it is caught and passed to
catchFunc for analysis. If catchFunc returns
false, the exception is rethrown. Regardless of the outcome,
finallyFunc, if specified, is invoked as a final step before the
function exits.
Examples
The following code demonstrates handling host exceptions in script code.
It assumes that an instance of
ExtendedHostFunctions is exposed under
the name "host"
(see
AddHostObject).
var ConsoleT = host.type("System.Console");
var WebClientT = host.type("System.Net.WebClient", "System");
var webClient = host.newObj(WebClientT);
host.tryCatch(
function () {
ConsoleT.WriteLine(webClient.DownloadString("http://cnn.com"));
},
function (exception) {
ConsoleT.WriteLine("*** ERROR: " + exception.GetBaseException().ToString());
return true;
},
function () {
ConsoleT.WriteLine("*** CLEANING UP ***");
webClient.Dispose();
}
);
See Also