{"record":{"id":"e8287c764738dd30","repo":"dotnet/maui","slug":"this-is-a-test-exception-thrown-from-c-code","errorCode":null,"errorMessage":"This is a test exception thrown from C# code!","messagePattern":"This is a test exception thrown from C# code!","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Controls/samples/Controls.Sample/Pages/Controls/HybridWebViewPage.xaml.cs","lineNumber":207,"sourceCode":"\t\t\t\treturn \"Hello from C#!\";\n\t\t\t}\n\n\t\t\tpublic async Task<SyncReturn> DoAsyncWorkParamsReturn(int i, string s)\n\t\t\t{\n\t\t\t\tawait Task.Delay(1000);\n\t\t\t\tDebug.WriteLine($\"DoAsyncWorkParamsReturn: {i}, {s}\");\n\t\t\t\treturn new SyncReturn\n\t\t\t\t{\n\t\t\t\t\tMessage = \"Hello from C#! \" + s,\n\t\t\t\t\tValue = i,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Demo method that throws an exception to showcase error handling\n\t\t\tpublic void ThrowException()\n\t\t\t{\n\t\t\t\tDebug.WriteLine(\"ThrowException called - about to throw\");\n\t\t\t\tthrow new InvalidOperationException(\"This is a test exception thrown from C# code!\");\n\t\t\t}\n\n\t\t\t// Demo async method that throws an exception\n\t\t\tpublic async Task<string> ThrowExceptionAsync()\n\t\t\t{\n\t\t\t\tDebug.WriteLine(\"ThrowExceptionAsync called - about to throw\");\n\t\t\t\tawait Task.Delay(100);\n\t\t\t\tthrow new ArgumentException(\"This is an async test exception thrown from C# code!\");\n\t\t\t}\n\t\t}\n\n\t\tpublic class SyncReturn\n\t\t{\n\t\t\tpublic string? Message { get; set; }\n\t\t\tpublic int Value { get; set; }\n\t\t}\n\t}\n}","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Controls/samples/Controls.Sample/Pages/Controls/HybridWebViewPage.xaml.cs#L189-L225","documentation":"This is an intentional demo exception inside a HybridWebView sample's proxy/raw method dispatcher. The ThrowException() method is a showcase of how unhandled C# exceptions surface back to JavaScript when called through HybridWebView's JS-to-C# bridge. It is not a production error — it is deliberately thrown to demonstrate error propagation.","triggerScenarios":"JavaScript code in the HybridWebView calls the proxy method `ThrowException()` (or the async variant `ThrowExceptionAsync()`). The C# method throws InvalidOperationException which the HybridWebView bridge marshals back to the JS caller as a rejected promise or error callback.","commonSituations":"Running the HybridWebView sample and clicking the demo button that invokes the ThrowException method. This is expected behavior to test/showcase error handling across the JS-C# boundary.","solutions":["If you are in the sample, this is expected — use it to verify your JS error handling displays the C# exception message.","Remove or comment out the call to ThrowException in production code that was copied from the sample.","In your own HybridWebView proxy methods, replace `throw new InvalidOperationException(...)` with proper error handling/logging if you do not want exceptions propagated to JS."],"exampleFix":"// before (sample demo — intentionally throws)\npublic void ThrowException()\n{\n    throw new InvalidOperationException(\"This is a test exception thrown from C# code!\");\n}\n\n// after (production-safe)\npublic void ThrowException()\n{\n    Debug.WriteLine(\"ThrowException requested — handling gracefully instead of throwing.\");\n    // handle the error case without throwing\n}","handlingStrategy":"try-catch","validationCode":"// This is intentional demo code — no validation needed in the sample.\n// In production, avoid throwing from methods exposed to JS:\npublic void ThrowException()\n{\n    // Handle the error case gracefully instead of throwing\n    Debug.WriteLine(\"Error condition handled gracefully.\");\n}","typeGuard":null,"tryCatchPattern":"// JavaScript-side: catch the error from the C# proxy\ntry {\n    await window.HybridWebView.InvokeDotNetAsync('ThrowException');\n} catch (e) {\n    console.error('C# threw:', e.message); // 'This is a test exception thrown from C# code!'\n}","preventionTips":["Recognize this is sample demo code, not a production error.","When copying HybridWebView proxy methods to production, replace throws with proper error handling.","Implement JS-side try-catch around all InvokeDotNet calls to handle C# exceptions gracefully."],"tags":["maui","hybridwebview","sample","demo","javascript-interop","intentional"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}