Type Function Library (globals) Return value Boolean Revision Release 2022.3683 Keywords pcall, protected mode, protected call, functions See also (globals)
Calls a function specified as the first argument, with the given arguments (as the following 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.
pcall( f [, ...] )
Function. The function to be called in protected mode.
Any. Can be anywhere from zero to any amount of arguments to be passed to the function to be called.
In the following example, the function will fail and normally stop the simulator because we are trying to concatenate a nil
value. The pcall()
will catch and display the error without stopping the simulator.
function myPrint( value ) local foo = value .. nil end print( pcall( myPrint, "hello" ) ) -- print false and error message