Capricorn 76
This Lua tutorial shows how to add error handling to scripts.


--------------------------------
--------------------------------
-- Error Handling: Test 1
-- This example shows how to handle errors that are raised inside ScriptENGINE states.
--
-- Copyright (c) 2006-2008 Capricorn 76 Pty. Ltd.
--------------------------------
--------------------------------

---------------------------
---------------------------
-- PROGRAM STARTS HERE
-- All global code (outside of any functions) is first run.
-- Then the ScriptENGINE automatically run the State Machine by looking for the OnStateRun_[scriptName]() function.
-- If its not found (in this case it is), then the ScriptENGINE will look for the fallback function OnStateRun()
-- Notice that it's name is derived from the filename of this script.
---------------------------
---------------------------
function OnStateRun_test1(param1, param2, param3)
    -- Open the console, so we can display a message to the user
    IApp:showConsoleWindow();

    -- Queue a couple of state
    IState:queue('test2_ManualError');
    IState:queue('test2_E76Error');
end

--------------------------------
-- A state that raises an error manually
--------------------------------
function OnStateRun_test2_ManualError(param1, param2, param3)
    -- Wait for user to press a key
    print('\nAbout to raise a run-time error. Press any key...');
    IKey:getch();

    -- We can create run-time errors in our scripts,
    -- but errors may be raised by ScriptENGINE, so error-handlers are very useful.
    error('Manually created error');
end

--------------------------------
-- State error handler
--------------------------------
function OnStateRun_test2_ManualError_OnError(errorString, functionName, sourceFilename, lineNumber)

    -- The extra error information is only available when the error is raised from ScriptENGINE
    print('Handled the error:', tostring(errorString));
    print('Error raise from:', functionName);
    print('Error file:', sourceFilename);
    print('Error line:', lineNumber);

    -- Wait for user to press a key
    print('Press any key to continue...');
    IKey:getch();

    -- Tell ScriptENGINE that we handled the error.
    -- No default processing will occur.
    return true;
end

--------------------------------
-- A state that raises an error from inside ScriptENGINE
--------------------------------
function OnStateRun_test2_E76Error(param1, param2, param3)
    print('\nAbout to use the graphics engine without initialising it. Press any key...');
    IKey:getch();
    IGraphics:getControlText('abcd');
end

--------------------------------
-- State error handler
--------------------------------
function OnStateRun_test2_E76Error_OnError(errorString, functionName, sourceFilename, lineNumber)

    -- The extra error information is only available when the error is raised from ScriptENGINE
    print('Handled the error:', tostring(errorString));
    print('Error raise from:', functionName);
    print('Error file:', sourceFilename);
    print('Error line:', lineNumber);

    -- Wait for user to press a key
    IKey:getch();

    -- Tell ScriptENGINE that we handled the error.
    -- No default processing will occur.
    return true;
end

Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:11 2009)