Tabletop Simulator

Tabletop Simulator

View Stats:
Lili May 14, 2017 @ 12:51pm
Use of pcall for error handling
I'm trying to use pcall for handling an error but it's not working.
I run this script:

function faultyFunction()
error('This function returns an error')
end

function onLoad()
print(pcall(faultyFunction()))
end

Suposedly it should print false since the function returns an error, but i just get this:
"Error in Script (Global) function <onload>: chunk_1:(2,9-43): This function returns an error"

Am I doing something wrong?
< >
Showing 1-3 of 3 comments
FragaholiC May 14, 2017 @ 1:40pm 
The usage of pcall is
pcall(yourFunction, argument1_for_yourFunction, arg2, ...)
You shouldn't call your faultyFunction inside the argument list of pcall but rather submit the function as a variable.
faultyFuntcion -- variable of your function faultyFunction() -- with parenthesis you are calling the function

Long story short, use the function without parenthesis as an argument in pcall. The next arguments of pcall are used as arguments for the function you call with pcall.

function faultyFunction() error('This function returns an error') end function onLoad() print(pcall(faultyFunction)) -- no parenthesis, just faultyFunction without () end
Lili May 14, 2017 @ 1:56pm 
Yay, it works. Thank you so much
FragaholiC May 14, 2017 @ 1:59pm 
You are welcome.
< >
Showing 1-3 of 3 comments
Per page: 1530 50

Date Posted: May 14, 2017 @ 12:51pm
Posts: 3