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

[Solved] Catch error, display custom error message

Started by LoganDark, 17 June 2016 - 11:50 PM
LoganDark #1
Posted 18 June 2016 - 01:50 AM
I want to use shell.run to run a program that may not exist, and I have my own error message that I want to display instead of "No such program". Is that possible?
Edited on 18 June 2016 - 12:00 AM
theoriginalbit #2
Posted 18 June 2016 - 01:55 AM
Error handling and Exceptions over at the Lua manual will point you in the direction of pcall.

It would go a little like this

local ok, err = pcall(function() shell.run("program") end)

if not ok then
  print("No program")
end
Edited on 17 June 2016 - 11:55 PM
LoganDark #3
Posted 18 June 2016 - 01:56 AM
Error handling and Exceptions over at the Lua manual will point you in the direction of pcall.

It would go a little like this

local ok, err = pcall(function() shell.run("program") end)

if not ok then
  print("No program")
end

Okay, thanks. I forgot about pcall.
Bomb Bloke #4
Posted 18 June 2016 - 02:16 AM
shell.run() doesn't actually error if the targeted file doesn't exist. You'd be better off using fs.exists() to check before attempting to run in the first place.
LoganDark #5
Posted 18 June 2016 - 02:18 AM
shell.run() doesn't actually error if the targeted file doesn't exist. You'd be better off using fs.exists() to check before attempting to run in the first place.

Oh, okay.

Edit: So fs.combine shell.dir() and what to run?

Edit 2: I guess that's a yes
Edited on 18 June 2016 - 12:25 AM