Skip to content

Commit 4014e40

Browse files
Copilotmattleibow
andcommitted
Add demo exception handling to HybridWebView sample
- Added ThrowException and ThrowExceptionAsync methods to demonstrate new functionality - Added JavaScript functions to test and showcase exception catching - Added UI buttons to trigger exception handling tests - Updated sample to show both error and success scenarios Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
1 parent 177dc53 commit 4014e40

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/Controls/samples/Controls.Sample/Pages/Controls/HybridWebViewPage.xaml.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ public async Task<SyncReturn> DoAsyncWorkParamsReturn(int i, string s)
153153
Value = i,
154154
};
155155
}
156+
157+
// Demo method that throws an exception to showcase error handling
158+
public void ThrowException()
159+
{
160+
Debug.WriteLine("ThrowException called - about to throw");
161+
throw new InvalidOperationException("This is a test exception thrown from C# code!");
162+
}
163+
164+
// Demo async method that throws an exception
165+
public async Task<string> ThrowExceptionAsync()
166+
{
167+
Debug.WriteLine("ThrowExceptionAsync called - about to throw");
168+
await Task.Delay(100);
169+
throw new ArgumentException("This is an async test exception thrown from C# code!");
170+
}
156171
}
157172

158173
public class SyncReturn

src/Controls/samples/Controls.Sample/Resources/Raw/HybridSamplePage/index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,32 @@
9393
LogMessage("Invoked DoAsyncWorkParamsReturn, return value: message=" + retValue.Message + ", value=" + retValue.Value);
9494
}
9595

96+
// Demo functions to test exception handling
97+
async function InvokeThrowException() {
98+
LogMessage("Invoking ThrowException");
99+
try {
100+
await window.HybridWebView.InvokeDotNet('ThrowException');
101+
LogMessage("ThrowException unexpectedly succeeded!");
102+
} catch (ex) {
103+
LogMessage("Caught exception from ThrowException: " + ex.message);
104+
LogMessage("Exception type: " + (ex.dotNetErrorType || "Unknown"));
105+
if (ex.dotNetStackTrace) {
106+
LogMessage("Stack trace: " + ex.dotNetStackTrace.substring(0, 200) + "...");
107+
}
108+
}
109+
}
110+
111+
async function InvokeThrowExceptionAsync() {
112+
LogMessage("Invoking ThrowExceptionAsync");
113+
try {
114+
const result = await window.HybridWebView.InvokeDotNet('ThrowExceptionAsync');
115+
LogMessage("ThrowExceptionAsync unexpectedly succeeded with result: " + result);
116+
} catch (ex) {
117+
LogMessage("Caught async exception from ThrowExceptionAsync: " + ex.message);
118+
LogMessage("Exception type: " + (ex.dotNetErrorType || "Unknown"));
119+
}
120+
}
121+
96122
</script>
97123
</head>
98124
<body>
@@ -114,6 +140,11 @@
114140
<button onclick="InvokeDoAsyncWorkReturn()">Call C# async method (no params) and get simple return value</button>
115141
<button onclick="InvokeDoAsyncWorkParamsReturn()">Call C# async method (params) and get complex return value</button>
116142
</div>
143+
<div>
144+
<strong>Exception Handling Tests:</strong>
145+
<button onclick="InvokeThrowException()">Test C# Exception Handling</button>
146+
<button onclick="InvokeThrowExceptionAsync()">Test C# Async Exception Handling</button>
147+
</div>
117148
<div>
118149
Log: <textarea readonly id="messageLog" style="width: 80%; height: 10em;"></textarea>
119150
</div>

0 commit comments

Comments
 (0)