Difference between revisions of "Pcall"

From GiderosMobile
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
+
'''Available since:''' Gideros 2011.6<br/>
'''<translate>Class</translate>:''' [[Special:MyLanguage/(global)|(global)]]<br/>
+
'''Class:''' [[(global)]]<br/>
=== <translate>Description</translate> ===
+
 
<translate>Calls function f with the given arguments in protected mode. This means that any error inside f is not propagated; instead, [[Special:MyLanguage/pcall|pcall]] catches the error and returns a status code. Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, [[Special:MyLanguage/pcall|pcall]] also returns all results from the call, after this first result. In case of any error, [[Special:MyLanguage/pcall|pcall]] returns false plus the error message.</translate>
+
=== Description ===
<source lang="lua">
+
Calls function ''f'' with the given arguments in protected mode. This means that any error inside ''f'' is not propagated, instead '''pcall''' catches the error and returns a status code.
 +
<syntaxhighlight lang="lua">
 
(bool), (any) = pcall(f,arg1,arg2,...)
 
(bool), (any) = pcall(f,arg1,arg2,...)
</source>
+
</syntaxhighlight>
=== <translate>Parameters</translate> ===
+
 
'''f''': (function) <translate>function to call in protected mode</translate> <br/>
+
Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, '''pcall''' also returns all results from the call, after this first result. In case of any error, '''pcall''' returns false plus the "''error''" message.
'''arg1''': (any) <translate>argument to pass to the function</translate> '''optional'''<br/>
+
 
'''arg2''': (any) <translate>argument to pass to the function</translate> '''optional'''<br/>
+
=== Parameters ===
'''...''': (any) <translate>other optional arguments</translate> '''optional'''<br/>
+
'''f''': (function) function to call in protected mode<br/>
=== <translate>Return values</translate> ===
+
'''arg1''': (any) argument to pass to the function '''optional'''<br/>
'''<translate>Returns</translate>''' (bool) <translate>false if there was error, true if function call succeeded</translate><br/>
+
'''arg2''': (any) argument to pass to the function '''optional'''<br/>
'''<translate>Returns</translate>''' (any) <translate>all the results that function returns</translate><br/>
+
'''...''': (any) other optional arguments '''optional'''<br/>
 +
 
 +
=== Return values ===
 +
'''Returns''' (bool) false if there was error, true if function call succeeded<br/>
 +
'''Returns''' (any) all the results that function returns<br/>
 +
 
 +
=== Example ===
 +
<syntaxhighlight lang="lua">
 +
function isValidTexture()
 +
local result, value = pcall(Texture.new, "gfx/btns/end_down.png")
 +
return result, value
 +
end
 +
 
 +
local bmp
 +
 
 +
local result, value = isValidTexture()
 +
if result then
 +
bmp = Bitmap.new(value)
 +
stage:addChild(bmp)
 +
else
 +
print(result, value)
 +
end
 +
</syntaxhighlight>
 +
 
 +
=== See Also ===
 +
'''[[Assert]]'''
 +
 
 +
{{(global)}}

Latest revision as of 10:54, 5 October 2023

Available since: Gideros 2011.6
Class: (global)

Description

Calls function f with the given arguments in protected mode. This means that any error inside f is not propagated, instead pcall catches the error and returns a status code.

(bool), (any) = pcall(f,arg1,arg2,...)

Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, pcall also returns all results from the call, after this first result. In case of any error, pcall returns false plus the "error" message.

Parameters

f: (function) function to call in protected mode
arg1: (any) argument to pass to the function optional
arg2: (any) argument to pass to the function optional
...: (any) other optional arguments optional

Return values

Returns (bool) false if there was error, true if function call succeeded
Returns (any) all the results that function returns

Example

function isValidTexture()
	local result, value = pcall(Texture.new, "gfx/btns/end_down.png")
	return result, value
end

local bmp

local result, value = isValidTexture()
if result then
	bmp = Bitmap.new(value)
	stage:addChild(bmp)
else
	print(result, value)
end

See Also

Assert