Garry's Mod Wiki

xpcall

boolean, vararg xpcall( function func, function errorCallback, vararg arguments )

Description

Attempts to call the first function. If the execution succeeds, this returns true followed by the returns of the function. If execution fails, this returns false and the second function is called with the error message.

Unlike in pcall, the stack is not unwound and can therefore be used for stack analyses with the debug.

Using this function with include will break autorefresh.
This cannot stop errors from hooks called from the engine.
This does not stop Error and ErrorNoHalt from sending error messages to the server (if called clientside) or calling the GM:OnLuaError hook. The success boolean returned will always return true and thus you will not get the error message returned. error does not exhibit these behaviours.
This does not stop errors incurred by include.

Arguments

1 function func
The function to call initially.
2 function errorCallback
The function to be called if execution of the first fails; the error message is passed as a string.

You cannot throw an error() from this callback: it will have no effect (not even stopping the callback).

3 vararg arguments
Arguments to pass to the initial function.

Returns

1 boolean
Status of the execution; true for success, false for failure.
2 vararg
The returns of the first function if execution succeeded, otherwise the first return value of the error callback.

Example

Using xpcall to catch an error.

local function test() aisj() end local function catch( err ) print( "ERROR: ", err ) end print( "Output: ", xpcall( test, catch ) )
Output:
ERROR: lua/wiki/xpcall_example.lua:2: attempt to call global 'aisj' (a nil value) Output: false nil

Page Links


Special Pages


Wikis

?

Render Time: 54ms

DB GetPage 3
Generate Html 27
SaveChanges (1) 9
Render Body 0
Render Sidebar 12