ClearScript Library
ClearScript Library
ClearScript Library Reference
Microsoft.ClearScript
HostFunctions Class
HostFunctions Methods
asType<T> Method
cast<T> Method
del<T> Method
flags<T> Method
func Method
getElement Method
getProperty Method
isNull Method
isType<T> Method
isTypeObj Method
newArr Method
newObj Method
newVar<T> Method
proc Method
removeElement Method
removeProperty Method
setElement Method
setProperty Method
toByte Method
toChar Method
toDecimal Method
toDouble Method
toInt16 Method
toInt32 Method
toInt64 Method
toSByte Method
toSingle Method
toStaticType Method
toUInt16 Method
toUInt32 Method
toUInt64 Method
tryCatch Method
typeOf Method

# HostFunctionstryCatch Method

Allows script code to handle host exceptions.
Namespace: Microsoft.ClearScript
Assembly: ClearScript.Core (in ClearScript.Core.dll) Version: 7.5.0
Syntax
C#
VB
C++
F#
Copy

    public 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 Object
  A script function that invokes one or more host methods or properties.
catchFunc Object
  A script function to invoke if tryFunc throws an exception.
finallyFunc Object (Optional)
  An optional script function that performs cleanup for the operation.

#### Return Value

Boolean
True 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.
Example
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).
JavaScript
Copy

    // import types
    var ConsoleT = host.type("System.Console");
    var WebClientT = host.type("System.Net.WebClient", "System");
    // create a Web client
    var webClient = host.newObj(WebClientT);
    host.tryCatch(
        function () {
            // download Web document
            ConsoleT.WriteLine(webClient.DownloadString("http://cnn.com"));
        },
        function (exception) {
            // dump exception
            ConsoleT.WriteLine("*** ERROR: " + exception.GetBaseException().ToString());
            return true;
        },
        function () {
            // clean up
            ConsoleT.WriteLine("*** CLEANING UP ***");
            webClient.Dispose();
        }
    );

See Also

#### Reference

HostFunctions Class
Microsoft.ClearScript Namespace
type(String, String, Object)
type(String, Object)

Copyright © Microsoft Corporation. All rights reserved.

Send comments on this topic to Microsoft