How to catch a Lua exception in C#

1.9k Views Asked by At

I am using the an assembly named LuaInterface to run lua-code inside my C# application. During the lua execution I create some WinForms & map event handlers (lua-methods) to them.

The problem is that the doString (aka runLuaCode) method only runs the init routine and the constructors. This is fine and intended, however the doString function acts non blocking so the function returns while the Lua-created-Forms are still there. This means that any exception (null-ref and alike) which is not raised during the constructor is not handled by the lua error handling an crashes all the way up to my wndProc of my Editor - which most likely kills my editor and make error handling virtually impossible.

Is there any way to create a new Thread / Process / AppDomain that handles it's own WndProc so that only this sub-task needs to handle the exceptions?

Should I block my Editor at the doString with a while loop in lua until the forms are closed?

What other options do I have?

Any advice on this matter is greatly appreciated!

1

There are 1 best solutions below

2
On

Another Lua enthusiast!! Finally! :) I am also toying with the idea to use Lua for macro scripting in my .NET apps.

I am not sure I get it. I wrote some sample code and it seems to be working ok. Simple try catch around DoString gets the LuaExceptions. DoString does block the main thread unless you explicitly create a new thread. In case of a new thread normal .NET multithreaded exception handling rules apply.

Example:

public const string ScriptTxt = @"
luanet.load_assembly ""System.Windows.Forms""
luanet.load_assembly ""System.Drawing""

Form = luanet.import_type ""System.Windows.Forms.Form""
Button = luanet.import_type ""System.Windows.Forms.Button""
Point = luanet.import_type ""System.Drawing.Point""
MessageBox = luanet.import_type ""System.Windows.Forms.MessageBox""
MessageBoxButtons = luanet.import_type ""System.Windows.Forms.MessageBoxButtons""

form = Form()
form.Text = ""Hello, World!""
button = Button()
button.Text = ""Click Me!""
button.Location = Point(20,20)
button.Click:Add(function()
        MessageBox:Show(""Clicked!"", """", MessageBoxButtons.OK) -- this will throw an ex
    end)   
form.Controls:Add(button)
form:ShowDialog()";

        private static void Main(string[] args)
        {
            try
            {
                var lua = new Lua();
                lua.DoString(ScriptTxt);
            }
            catch(LuaException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch(Exception ex)
            {
                if (ex.Source == "LuaInterface")
                {
                    Console.WriteLine(ex.Message);
                }
                else
                {
                    throw;
                }
            }

            Console.ReadLine();
        } 

LuaInterface has a pretty good documentation where tricky error handling is explained.

http://penlight.luaforge.net/packages/LuaInterface/#T6

I hope it helps. :)