/// <summary>
        ///     Returns the exception that caused the runtime of the current context to be in the
        ///     exception state and resets the exception state for that runtime.
        /// </summary>
        /// <remarks>
        ///     <para>
        ///     If the runtime of the current context is not in an exception state, this API will throw
        ///     <c>JsErrorInvalidArgument</c>. If the runtime is disabled, this will return an exception
        ///     indicating that the script was terminated, but it will not clear the exception (the
        ///     exception will be cleared if the runtime is re-enabled using
        ///     <c>EnableRuntimeExecution</c>).
        ///     </para>
        ///     <para>
        ///     Requires an active script context.
        ///     </para>
        /// </remarks>
        /// <returns>The exception for the runtime of the current context.</returns>
        public static JavaScriptValue GetAndClearException()
        {
            JavaScriptValue reference;

            Native.ThrowIfError(Native.JsGetAndClearException(out reference));
            return(reference);
        }
Example #2
0
        private static string GetErrorMessage()
        {
            // Get error message and clear exception
            JavaScriptValue exception;

            if (Native.JsGetAndClearException(out exception) != JavaScriptErrorCode.NoError)
            {
                return("failed to get and clear exception");
            }

            JavaScriptPropertyId messageName;

            if (Native.JsGetPropertyIdFromName("message",
                                               out messageName) != JavaScriptErrorCode.NoError)
            {
                return("failed to get error message id");
            }

            JavaScriptValue messageValue;

            if (Native.JsGetProperty(exception, messageName, out messageValue)
                != JavaScriptErrorCode.NoError)
            {
                return("failed to get error message");
            }

            IntPtr  message;
            UIntPtr length;

            if (Native.JsStringToPointer(messageValue, out message, out length) != JavaScriptErrorCode.NoError)
            {
                return("failed to convert error message");
            }

            return(Marshal.PtrToStringUni(message));
        }