Jump to content




[Solved] Catch error, display custom error message


  • You cannot reply to this topic
4 replies to this topic

#1 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 18 June 2016 - 12: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 by LoganDark, 18 June 2016 - 01:00 AM.


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 June 2016 - 12: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 by theoriginalbit, 18 June 2016 - 12:55 AM.


#3 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 18 June 2016 - 12:56 AM

View Posttheoriginalbit, on 18 June 2016 - 12:55 AM, said:

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.

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 18 June 2016 - 01: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.

#5 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 18 June 2016 - 01:18 AM

View PostBomb Bloke, on 18 June 2016 - 01:16 AM, said:

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 by LoganDark, 18 June 2016 - 01:25 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users