Difference between revisions of "Pcall"

From GiderosMobile
(added example)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
 
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
 
'''<translate>Class</translate>:''' [[Special:MyLanguage/(global)|(global)]]<br/>
 
  
=== <translate>Description</translate> ===
+
'''Available since:''' Gideros 2011.6<br/>
<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>
+
'''Class:''' [[Special:MyLanguage/(global)|(global)]]<br/>
 +
 
 +
=== 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. 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.
 
<source lang="lua">
 
<source lang="lua">
 
(bool), (any) = pcall(f,arg1,arg2,...)
 
(bool), (any) = pcall(f,arg1,arg2,...)
 
</source>
 
</source>
  
=== <translate>Parameters</translate> ===
+
=== Parameters ===
'''f''': (function) <translate>function to call in protected mode</translate> <br/>
+
'''f''': (function) function to call in protected mode <br/>
'''arg1''': (any) <translate>argument to pass to the function</translate> '''optional'''<br/>
+
'''arg1''': (any) argument to pass to the function '''optional'''<br/>
'''arg2''': (any) <translate>argument to pass to the function</translate> '''optional'''<br/>
+
'''arg2''': (any) argument to pass to the function '''optional'''<br/>
'''...''': (any) <translate>other optional arguments</translate> '''optional'''<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/>
  
=== <translate>Return values</translate> ===
+
=== Example ===
'''<translate>Returns</translate>''' (bool) <translate>false if there was error, true if function call succeeded</translate><br/>
+
<source lang="lua">
'''<translate>Returns</translate>''' (any) <translate>all the results that function returns</translate><br/>
+
local spath = "gfx/myimage.jpj" -- notice the error jpj instead of jpg
 +
local result, msg = pcall(Texture.new, spath, "file is not recognised")
 +
print(result, msg)
 +
</source>
  
 
{{(global)}}
 
{{(global)}}

Revision as of 00:16, 15 November 2020


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

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

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

local spath = "gfx/myimage.jpj" -- notice the error jpj instead of jpg
local result, msg = pcall(Texture.new, spath, "file is not recognised")
print(result, msg)