{"record":{"id":"b4868266a99fcc92","repo":"dotnet/maui","slug":"this-is-a-test-error-thrown-from-javascript-code","errorCode":null,"errorMessage":"This is a test error thrown from JavaScript code!","messagePattern":"This is a test error thrown from JavaScript code!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"info","filePath":"src/Controls/samples/Controls.Sample/Resources/Raw/HybridSamplePage/index.html","lineNumber":125,"sourceCode":"                }\n            }\n        }\n\n        async function InvokeThrowExceptionAsync() {\n            LogMessage(\"Invoking ThrowExceptionAsync\");\n            try {\n                const result = await window.HybridWebView.InvokeDotNet('ThrowExceptionAsync');\n                LogMessage(\"ThrowExceptionAsync unexpectedly succeeded with result: \" + result);\n            } catch (ex) {\n                LogMessage(\"Caught async exception from ThrowExceptionAsync: \" + ex.message);\n                LogMessage(\"Exception type: \" + (ex.dotNetErrorType || \"Unknown\"));\n            }\n        }\n\n        // JavaScript functions that throw exceptions (for testing C# exception handling of JS calls)\n        function ThrowJavaScriptError() {\n            LogMessage(\"ThrowJavaScriptError called - about to throw\");\n            throw new Error(\"This is a test error thrown from JavaScript code!\");\n        }\n\n        async function ThrowJavaScriptErrorAsync() {\n            LogMessage(\"ThrowJavaScriptErrorAsync called - about to throw\");\n            await new Promise(resolve => setTimeout(resolve, 100));\n            throw new Error(\"This is an async test error thrown from JavaScript code!\");\n        }\n\n    </script>\n</head>\n<body>\n    <div>\n        Hybrid sample!\n    </div>\n    <div>\n        <button onclick=\"window.HybridWebView.SendRawMessage('Message from JS! ' + (count++))\">Send message to C#</button>\n    </div>\n    <div>","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/samples/Controls.Sample/Resources/Raw/HybridSamplePage/index.html#L107-L143","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["If you reused the sample HTML, remove ThrowJavaScriptError or unwire it from production buttons.","If testing, keep C# JS-evaluation calls wrapped in try/catch (the bridge surfaces JS errors as exceptions on the C# side)."],"exampleFix":"// before\nfunction ThrowJavaScriptError() {\n  LogMessage(\"ThrowJavaScriptError called - about to throw\");\n  throw new Error(\"This is a test error thrown from JavaScript code!\");\n}\n\n// after\nfunction DoRealWork() {\n  LogMessage(\"DoRealWork called\");\n  return \"ok\";\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// C# side: JS-evaluation errors surface as exceptions\ntry\n{\n    await hybridWebView.EvaluateJavaScriptAsync(\"ThrowJavaScriptError()\");\n}\ncatch (Exception ex)\n{\n    // bridge surfaced the JS Error\n    Debug.WriteLine($\"JS threw: {ex.Message}\");\n}","preventionTips":["Remove or unwire demo ThrowJavaScriptError before shipping sample-derived HTML.","Wrap all C# EvaluateJavaScriptAsync calls in try/catch since JS can throw at any time.","Name test-only JS functions clearly (e.g. prefixed with Test_) to avoid accidental invocation."],"tags":["hybridwebview","javascript","sample","exception-handling"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}