Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


how to try catch exception [help]

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Lynxz Gaming
Expert Cheater
Reputation: 4

Joined: 01 Jul 2017
Posts: 208
Location: help

PostPosted: Sat Jul 21, 2018 5:15 am    Post subject: how to try catch exception [help] Reply with quote

Hello,

How to try catch exception
like we do in vb or c# but in lua

ty

_________________
my english is bad
discord : rynx#9828
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Sat Jul 21, 2018 5:35 am    Post subject: Reply with quote

pcall or xpcall, however iirc many functions provided by CE don't seem to support it (you'll get an error through the lua engine rather than a lua error placed on the stack that you can catch)... I practically never use it, preferring to instead write code that simply won't cause an error Very Happy
_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Jul 21, 2018 11:49 am    Post subject: Reply with quote

My modded version of someone elses implementation:

Code:

----------------------------------------------------------------------------------------------------
-- func: try (try, catch, finally)
-- desc: Implements C++ style try/catch handlers, and introduces C# style finally.
-- cred: djfdyuruiry
-- link: https://github.com/djfdyuruiry/lua-try-catch-finally/blob/master/try-catch-finally.lua
----------------------------------------------------------------------------------------------------
function try(trycb)
    local status, err = true, nil;

    -- Handle the try block if it is a proper function..
    if (type(trycb) == 'function') then
        status, err = xpcall(trycb, debug.traceback);
    end

    ------------------------------------------------------------------------------------------------
    -- func: finally
    -- desc: Implements the 'finally' callback handling for the try/catch/finally setup.
    ------------------------------------------------------------------------------------------------
    local finally = function(finallycb, hascatchcb)
        -- Invoke the finally callback if present..
        if (type(finallycb) == 'function') then
            finallycb();
        end

        -- Throw error if no catch callback is defined..
        if (not hascatchcb and not status) then
            error(err);
        end
    end

    ------------------------------------------------------------------------------------------------
    -- func: catch
    -- desc: Implements the 'catch' callback handling for the try/catch/finally setup.
    ------------------------------------------------------------------------------------------------
    local catch = function(catchcb)
        local hascatchcb = type(catchcb) == 'function';
       
        -- Invoke the catch callback if valid and an error was caught..
        if (not status and hascatchcb) then
            local e = err or '(Unknown Error)';
            catchcb(e);
        end

        -- Return a table exposing the finally handler..
        return {
            finally = function(finallycb)
                finally(finallycb, hascatchcb);
            end
        };
    end

    -- Return a table exposing the catch and finally handlers..
    return {
        catch = catch,
        finally = function(finallycb)
            finally(finallycb, false);
        end
    };
end


Example usage from one of my hooks:
Code:

    ------------------------------------------------------------------------------------------------
    -- event: beginscene
    -- desc : Invoked when a Direct3D scene is beginning.
    -- ret  : (No returns.)
    ------------------------------------------------------------------------------------------------
    ['beginscene'] = {
        name = 'beginscene',
        default = nil,
        handler = function(self, callbacks, ...)
            -- Loop and invoke each callback..
            for k, v in ipairs(callbacks) do
                try(function()
                    v.c();
                end).catch(function(e)
                    print(string.format('Event callback error was caught. (%s:%s) Error: %s', self.name, k, e));
                end);
            end
        end
    },

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Lynxz Gaming
Expert Cheater
Reputation: 4

Joined: 01 Jul 2017
Posts: 208
Location: help

PostPosted: Sat Jul 21, 2018 9:05 pm    Post subject: Reply with quote

atom0s wrote:
My modded version of someone elses implementation:

Code:

----------------------------------------------------------------------------------------------------
-- func: try (try, catch, finally)
-- desc: Implements C++ style try/catch handlers, and introduces C# style finally.
-- cred: djfdyuruiry
-- link: https://github.com/djfdyuruiry/lua-try-catch-finally/blob/master/try-catch-finally.lua
----------------------------------------------------------------------------------------------------
function try(trycb)
    local status, err = true, nil;

    -- Handle the try block if it is a proper function..
    if (type(trycb) == 'function') then
        status, err = xpcall(trycb, debug.traceback);
    end

    ------------------------------------------------------------------------------------------------
    -- func: finally
    -- desc: Implements the 'finally' callback handling for the try/catch/finally setup.
    ------------------------------------------------------------------------------------------------
    local finally = function(finallycb, hascatchcb)
        -- Invoke the finally callback if present..
        if (type(finallycb) == 'function') then
            finallycb();
        end

        -- Throw error if no catch callback is defined..
        if (not hascatchcb and not status) then
            error(err);
        end
    end

    ------------------------------------------------------------------------------------------------
    -- func: catch
    -- desc: Implements the 'catch' callback handling for the try/catch/finally setup.
    ------------------------------------------------------------------------------------------------
    local catch = function(catchcb)
        local hascatchcb = type(catchcb) == 'function';
       
        -- Invoke the catch callback if valid and an error was caught..
        if (not status and hascatchcb) then
            local e = err or '(Unknown Error)';
            catchcb(e);
        end

        -- Return a table exposing the finally handler..
        return {
            finally = function(finallycb)
                finally(finallycb, hascatchcb);
            end
        };
    end

    -- Return a table exposing the catch and finally handlers..
    return {
        catch = catch,
        finally = function(finallycb)
            finally(finallycb, false);
        end
    };
end


Example usage from one of my hooks:
Code:

    ------------------------------------------------------------------------------------------------
    -- event: beginscene
    -- desc : Invoked when a Direct3D scene is beginning.
    -- ret  : (No returns.)
    ------------------------------------------------------------------------------------------------
    ['beginscene'] = {
        name = 'beginscene',
        default = nil,
        handler = function(self, callbacks, ...)
            -- Loop and invoke each callback..
            for k, v in ipairs(callbacks) do
                try(function()
                    v.c();
                end).catch(function(e)
                    print(string.format('Event callback error was caught. (%s:%s) Error: %s', self.name, k, e));
                end);
            end
        end
    },


ooo thank you at0m

_________________
my english is bad
discord : rynx#9828
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites