Global/xpcall

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m
Line 1: Line 1:
 
{{Func
 
{{Func
|Name=xpcall
 
|Parent=Global
 
 
|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.  
 
|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 {{GlobalFunction|pcall}}, the stack is not unwound and can therefore be used for stack analyses with the {{Lib|debug}} library.
 
Unlike in {{GlobalFunction|pcall}}, the stack is not unwound and can therefore be used for stack analyses with the {{Lib|debug}} library.
  
'''NOTE:''' This does not stop {{GlobalFunction|Error}} and {{GlobalFunction|ErrorNoHalt}} from sending error messages to the server (if called clientside) or calling the {{HookFunction|GM|OnLuaError}} hook. The success boolean returned will always return true and thus you will not get the error message returned. {{GlobalFunction|error}} does not exhibit these behaviours.
+
{{Note|This does not stop {{GlobalFunction|Error}} and {{GlobalFunction|ErrorNoHalt}} from sending error messages to the server (if called clientside) or calling the {{HookFunction|GM|OnLuaError}} hook. The success boolean returned will always return true and thus you will not get the error message returned. {{GlobalFunction|error}} does not exhibit these behaviours.}}
 
|Realm=Shared
 
|Realm=Shared
 
|IsClass=No
 
|IsClass=No
 +
|Name=xpcall
 +
|Parent=Global
 
}}
 
}}
 
{{Arg
 
{{Arg

Revision as of 18:37, 27 April 2016

 xpcall( )

Contents

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 library library.

NOTE

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.

Arguments

function func

The function to call initially.

Arguments

function errorCallback

The function to be called if execution of the first fails; the error message is passed as a string.

Arguments

vararg arguments

Arguments to pass to the initial function.

Returns

boolean

Status of the execution; true for success, false for failure.

Returns

vararg

The returns of the first function if execution succeeded, otherwise the first return value of the error callback.

Examples

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
Personal tools
Navigation