ThreadMethods
self:try
Shared
Executes a function within a protected context with exception handling
Syntax
local ... = self:try(handles)Parameters
| Type | Name | Description |
|---|---|---|
| table | handles | Handles configuration containing the following fields: • exec (function) - primary function to execute• catch (function) - error handler invoked when an exception occurs |
Returns
| Type | Name | Description |
|---|---|---|
... | ... | Returned values from exec/catch handlers |
Callback
exec(self)| Type | Name | Description |
|---|---|---|
thread | self | Running virtualized thread instance |
Examples
local promise = thread:create_promise()
local self = thread:create(function(self)
local result = self:try({
exec = function(self)
local result = {self:await(promise)}
engine.print("Promise resolved:", table.unpack(result))
return "resolved"
end,
catch = function(...)
engine.print("Promise rejected:", ...)
return "rejected"
end
})
print("Try status:", result)
end)
self:resume()
timer:create(function()
promise.reject("Task failed", 456)
end, 5000, 1)