dotnet/maui · info · Error

This is a test error thrown from JavaScript code!

Error message

This is a test error thrown from JavaScript code!

What it means

A deliberate synchronous JavaScript Error thrown by the sample's ThrowJavaScriptError() function. It exists purely to test that C# can observe exceptions thrown by JS when C# invokes JavaScript via the HybridWebView bridge. It is sample/test code, not a library defect.

Source

Thrown at src/Controls/samples/Controls.Sample/Resources/Raw/HybridSamplePage/index.html:125

                }
            }
        }

        async function InvokeThrowExceptionAsync() {
            LogMessage("Invoking ThrowExceptionAsync");
            try {
                const result = await window.HybridWebView.InvokeDotNet('ThrowExceptionAsync');
                LogMessage("ThrowExceptionAsync unexpectedly succeeded with result: " + result);
            } catch (ex) {
                LogMessage("Caught async exception from ThrowExceptionAsync: " + ex.message);
                LogMessage("Exception type: " + (ex.dotNetErrorType || "Unknown"));
            }
        }

        // JavaScript functions that throw exceptions (for testing C# exception handling of JS calls)
        function ThrowJavaScriptError() {
            LogMessage("ThrowJavaScriptError called - about to throw");
            throw new Error("This is a test error thrown from JavaScript code!");
        }

        async function ThrowJavaScriptErrorAsync() {
            LogMessage("ThrowJavaScriptErrorAsync called - about to throw");
            await new Promise(resolve => setTimeout(resolve, 100));
            throw new Error("This is an async test error thrown from JavaScript code!");
        }

    </script>
</head>
<body>
    <div>
        Hybrid sample!
    </div>
    <div>
        <button onclick="window.HybridWebView.SendRawMessage('Message from JS! ' + (count++))">Send message to C#</button>
    </div>
    <div>

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. If you reused the sample HTML, remove ThrowJavaScriptError or unwire it from production buttons.
  2. If testing, keep C# JS-evaluation calls wrapped in try/catch (the bridge surfaces JS errors as exceptions on the C# side).

Example fix

// before
function ThrowJavaScriptError() {
  LogMessage("ThrowJavaScriptError called - about to throw");
  throw new Error("This is a test error thrown from JavaScript code!");
}

// after
function DoRealWork() {
  LogMessage("DoRealWork called");
  return "ok";
}
Defensive patterns

Strategy: try-catch

Try / catch

// C# side: JS-evaluation errors surface as exceptions
try
{
    await hybridWebView.EvaluateJavaScriptAsync("ThrowJavaScriptError()");
}
catch (Exception ex)
{
    // bridge surfaced the JS Error
    Debug.WriteLine($"JS threw: {ex.Message}");
}

Prevention

When it happens

Trigger: C# evaluates the function via the HybridWebView JS-evaluation API (e.g. EvaluateJavaScriptAsync("ThrowJavaScriptError()")), or a user clicks the sample's 'Test JS Exception' button which calls it.

Common situations: Running the sample's JS-exception test; copying the sample index.html into a real app and leaving the demo function wired to a button; automated tests that exercise every sample button.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/b4868266a99fcc92. Report an issue: GitHub.