 |
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
Lynxz Gaming Expert Cheater
Reputation: 4
Joined: 01 Jul 2017 Posts: 208 Location: help
|
Posted: Sat Jul 21, 2018 5:15 am Post subject: how to try catch exception [help] |
|
|
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 |
|
 |
FreeER Grandmaster Cheater Supreme
Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Sat Jul 21, 2018 5:35 am Post subject: |
|
|
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
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Jul 21, 2018 11:49 am Post subject: |
|
|
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 |
|
 |
Lynxz Gaming Expert Cheater
Reputation: 4
Joined: 01 Jul 2017 Posts: 208 Location: help
|
Posted: Sat Jul 21, 2018 9:05 pm Post subject: |
|
|
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 |
|
 |
|
|
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
|
|