Global/xpcall

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Added this page)
 
(Bug)
 
(23 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{Function
+
{{Func
| Name = pcall
+
|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.
| Namespace = [[Global]]
+
| Realm = Shared
+
}}
+
  
{{Syntax
+
Unlike in {{GlobalFunction|pcall}}, the stack is not unwound and can therefore be used for stack analyses with the {{Lib|debug}}.
| Name = pcall
+
| Description = Attempts to call the first function, if the execution fails, the second function is called with the error message, unlike in [[Global/pcall]] the stack is not unwinded and can therefor be used to stack analyses using {{LibraryFunction|debug|getinfo}}, the disadvantage of this function is, that it does not allow passing argument to the to be called function.
+
| Returns = {{Return|bool|success}}, {{Return|vararg|return values}} or {{Return|nil|nil}}
+
  
| Arg1Name = func
+
{{Bug|Issue=1976|Using this function with {{GlobalFunction|include}} will break autorefresh.}}
| Arg1Type = function
+
| Arg1Req = true
+
| Arg1Desc = Numbers to get the largest from.
+
  
| Arg2Name = errorCallback
+
{{Bug|Issue=2036|This cannot stop errors from hooks called from the engine.}}
| Arg2Type = function
+
 
| Arg2Req = true
+
{{Bug|Issue=2498|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.}}
| Arg2Desc = Arguments to call the function with.
+
 
 +
{{Bug|Issue=3112|This does not stop errors incurred by {{GlobalFunction|include}}.}}
 +
|Realm=Shared and Menu
 +
|IsClass=No
 
}}
 
}}
 +
{{Arg
 +
|type=function
 +
|name=func
 +
|desc=The function to call initially.
 +
}}
 +
{{Arg
 +
|type=function
 +
|name=errorCallback
 +
|desc=The function to be called if execution of the first fails; the error message is passed as a string.
  
==Examples==
+
You cannot throw an {{GlobalFunction|error}}() from this callback: it will have no effect (not even stopping the callback).
 +
}}
 +
{{Arg
 +
|type=vararg
 +
|name=arguments
 +
|desc=Arguments to pass to the initial function.
 +
}}
 +
{{Ret
 +
|type=boolean
 +
|desc=Status of the execution; ''true'' for success, ''false'' for failure.
 +
}}
 +
{{Ret
 +
|type=vararg
 +
|desc=The returns of the first function if execution succeeded, otherwise the '''first''' return value of the error callback.
 +
}}
 
{{Example
 
{{Example
| Description = Catch an error.
+
|Description=Using xpcall to catch an error.
| Code =
+
|Code=local function test()
local succ, arg1= xpcall(function() aisj() end, function(err) print("ERROR:", err) end)
+
aisj()
print(succ)
+
end
| Output = ERROR: SOURCE attempt to call global 'aisj' (a nil value)<br>
+
 
false
+
local function catch( err )
 +
print( "ERROR: ", err )
 +
end
 +
 
 +
print( "Output: ", xpcall( test, catch ) )
 +
|Output=<br/><br/><samp>ERROR: &#09;lua/wiki/xpcall_example.lua:2: attempt to call global 'aisj' (a nil value)
 +
Output:&#09;false &#09;nil</samp>
 
}}
 
}}

Latest revision as of 06:46, 26 February 2019

 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.

BUG

Using this function with include will break autorefresh.

Issue Tracker: #1976

BUG

This cannot stop errors from hooks called from the engine.

Issue Tracker: #2036

BUG

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.

Issue Tracker: #2498

BUG

This does not stop errors incurred by include.

Issue Tracker: #3112

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.

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

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