WebAssembly - can't catch errors

webassembly
island

(Chris) #1

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?


(Carlo Kok) #2

You aren’t doing anything wrong at all. The WebAssembly standard currently doesn’t support exceptions yet, so at the moment all exceptions are fatal. The idea was that this would solve itself within short time, but it doesn’t seem like there’s a spec for it yet so I might have to consider alternatives.


(Chris) #3

I was afraid you were going to say that. I was hoping that Island had some sort of work-around. Even just allowing code to continue would be a big step in the right direction, even if try/catch isn’t possible.

On a somewhat related note, I know GC isn’t supported in wasm either. Does Island or the compiler take care of that at all, or should I be destroying my objects manually?

Thanks


(Carlo Kok) #4

GC works fine yes.


(Chris) #5

One more question please?

In c# wasm is there anything like the Activator.CreateInstance method? I’m trying to write a simple parser and I need to be able to create instances of objects as information is parsed. However the type of those objects has to be passed into the parser (i.e. a generic method).

Thanks.


(Carlo Kok) #6

You can call Type.NewInstance to instantiate a type, but we don’t have a way to call a generic function via reflection with variable generic types (because that would requires a jit)


(Chris) #7

Couldn’t find Type.NewInstance but that put me on the right track. I ended up using Type.Instantiate().

You’ve been a big help. Thank you so much.