Vital.sandbox
ThreadMethods

self:try

Shared

Executes a function within a protected context with exception handling


Syntax

local ... = self:try(handles)

Parameters

TypeNameDescription
tablehandlesHandles configuration containing the following fields:
exec (function) - primary function to execute
catch (function) - error handler invoked when an exception occurs

Returns

TypeNameDescription
......Returned values from exec/catch handlers

Callback

exec(self)
TypeNameDescription
threadselfRunning 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)

On this page