I’m probably doing something wrong, but I can’t seem to catch any errors in my C# WebAssembly project.
Example:
public class TestException : Exception
{
public TestException(string message) : base(message)
{
}
}
public void Button1Click(Object sender)
{
try
{
throw (new TestException("test error!!!"));
}
catch (Exception e)
{
Console.WriteLine ($"Caught Error - {e}");
}
Console.WriteLine("Past error");
When run, I get an error in the browser’s console:
Exception: mpa.Program.TestException: test error!!!
RemObjectsElements.js:246 Uncaught RuntimeError: unreachable
at ElementsRaiseException (wasm-function[50]:74)
at mi_td_mpa_d_Programc_Button3Clickno (wasm-function[7199]:452)
at .Lmi_t33_RemObjects_d_Elements_d_RTL_d_Delphi_d_TNotifyEvent6_Invokeno (wasm-function[166]:63)
at .Lmi_tnt35_RemObjects_d_Elements_d_RTL_d_Delphi_d_VCL_d_TControl18__l__g_c____DisplayClass01e__l_PlatformSetOnClick_g_b____0nt31_RemObjects_d_Elements_d_System_d_EcmaScriptObject (wasm-function[1076]:43)
at .Lmi_t34_RemObjects_d_Elements_d_System_d_WebAssemblyDelegate6_Invokent31_RemObjects_d_Elements_d_System_d_EcmaScriptObject (wasm-function[6665]:63)
at __island_call_delegate (wasm-function[6703]:45)
at HTMLButtonElement.res (http://127.0.0.1:60148/wasm/RemObjectsElements.js:246:62)
ElementsRaiseException
mi_td_mpa_d_Programc_Button3Clickno
.Lmi_t33_RemObjects_d_Elements_d_RTL_d_Delphi_d_TNotifyEvent6_Invokeno
.Lmi_tnt35_RemObjects_d_Elements_d_RTL_d_Delphi_d_VCL_d_TControl18__l__g_c____DisplayClass01e__l_PlatformSetOnClick_g_b____0nt31_RemObjects_d_Elements_d_System_d_EcmaScriptObject
.Lmi_t34_RemObjects_d_Elements_d_System_d_WebAssemblyDelegate6_Invokent31_RemObjects_d_Elements_d_System_d_EcmaScriptObject
__island_call_delegate
res @ RemObjectsElements.js:246
Thinking I was clever, I added the following onerror function in JS and a matching OnError method in C#,
window.onerror = function(error, url, line)
{
console.log("JS OnError - " + error);
program.OnError(error);
}
public void OnError(string msg)
{
Console.WriteLine($"Program OnError Error - {msg}");
}
They do their thing, because when I run it again I get this in the browser console:
Fatal exception in WebAssembly!
RemObjectsElements.js:277 Exception: mpa.Program.TestException: test error!!!
index.html:27 JS OnError - Script error.
RemObjectsElements.js:277 Program OnError Error - Script error.
RemObjectsElements.js:246 Uncaught RuntimeError: unreachable
at ElementsRaiseException (wasm-function[50]:74)
at mi_td_mpa_d_Programc_Button3Clickno (wasm-function[7199]:452)
at .Lmi_t33_RemObjects_d_Elements_d_RTL_d_Delphi_d_TNotifyEvent6_Invokeno (wasm-function[166]:63)
at .Lmi_tnt35_RemObjects_d_Elements_d_RTL_d_Delphi_d_VCL_d_TControl18__l__g_c____DisplayClass01e__l_PlatformSetOnClick_g_b____0nt31_RemObjects_d_Elements_d_System_d_EcmaScriptObject (wasm-function[1076]:43)
at .Lmi_t34_RemObjects_d_Elements_d_System_d_WebAssemblyDelegate6_Invokent31_RemObjects_d_Elements_d_System_d_EcmaScriptObject (wasm-function[6665]:63)
at __island_call_delegate (wasm-function[6703]:45)
at HTMLButtonElement.res (http://127.0.0.1:60755/wasm/RemObjectsElements.js:246:62)
But I still have the same problem of the exception never making it to my catch statement.
I have both ‘Stop on Exceptions’ and ‘Stop on Other Exception when Testing’ unchecked in Water.
Any ideas what I’m doing wrong?