This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ryan0788's profile picture

pcall() Not Catching Error

Started by ryan0788, 11 March 2012 - 09:39 PM
ryan0788 #1
Posted 11 March 2012 - 10:39 PM
Hi, I'm trying to add error support for a file manager I'm making, but for some reason pcall() isn't catching an error if the file I'm trying to delete is restricted.

if pcall(fs.delete(fileOptionsFile)) then misc.popup("Successfully deleted file.", 2)
else misc.popup("Failed to delete file.", 2)
end
MysticT #2
Posted 11 March 2012 - 10:46 PM
You are calling the function fs.delete() and passing it's return value to pcall.
When using pcall, you should pass the parameters like this:

pcall(function, param1, param2, ...)
ryan0788 #3
Posted 11 March 2012 - 11:04 PM
Alright, but when I try:

if pcall(fs.delete(), fileOptionsFile) then misc.popup("Successfully deleted file.", 2)
else misc.popup("Failed to delete file.", 2)
end

It says "string expected, got nil" ? I made sure that fileOptionsFile is a string and that the error isn't in misc.popup()
MysticT #4
Posted 11 March 2012 - 11:06 PM
Just pass the function, don't call it, remove the ()
It should be like this:

if pcall(fs.delete, fileOptionsFile) then
    misc.popup("Successfully deleted file.", 2)
else
    misc.popup("Failed to delete file.", 2)
end
ryan0788 #5
Posted 11 March 2012 - 11:07 PM
Alright, thanks! :mellow:/>/>