Page 1 of 1

Love Error handling question

Posted: Fri Jan 13, 2023 7:40 pm
by elfrea
Hi, I just want to know if its possible to catch a love.graphics.newImage error (file not found) and manage what happens myself. in other words, i don't want the program to crash. something like that in C:

try {
image = love.graphics.newImage("test.png")
}

catch {
image = nil
}

What im doing right now is just chjecking if the file exists beforehand, but it wont tell me if theres another error opening the file

Re: Love Error handling question

Posted: Fri Jan 13, 2023 7:55 pm
by darkfrei

Re: Love Error handling question

Posted: Fri Jan 13, 2023 9:17 pm
by pgimeno
Also, for exception handling, see pcall.

Code: Select all

do
  local ok
  ok, image = pcall(love.graphics.newImage, "test.png")
  if not ok then image = nil end
end

Re: Love Error handling question

Posted: Fri Jan 13, 2023 9:55 pm
by elfrea
darkfrei : did you read my question? I was not asking about to check if a file exists or of any type, it's about exception handling. But thanks anyway :)

pgimeno : Thank you, thats exactly what i was looking for

Re: Love Error handling question

Posted: Sat Jan 14, 2023 8:42 am
by darkfrei
Debug without errors will be much harder :)

Re: Love Error handling question

Posted: Sat Jan 14, 2023 10:08 am
by pgimeno
Without errors? If there's a problem you can always debug it by printing 'image', which contains the error message when 'ok' is false. Or even temporarily wrap pcall() in an assert().

Code: Select all

-- possibility 1
do
  local ok
  ok, image = pcall(love.graphics.newImage, "test.png")
  if not ok then
    print("love.graphics.newImage returned this error: " .. image)
    image = nil
  end
end

-- possibility 2
do
  local ok
  ok, image = assert(pcall(love.graphics.newImage, "test.png"))
  if not ok then image = nil end
end
The first form has little difference with the version using love.filesystem.getInfo. The only major difference is that in the extremely rare case that getInfo says that the file exists, and love.graphics.newImage still produces an error, execution won't stop (but the error message will be displayed anyway).

The second form is basically equivalent to not having the pcall, so execution will stop similarly. This assert can be added temporarily if there's a bug around that and you want to examine whether it's failing and how.

Exception handling is basic. There's even a paradigm called "EAFP" (Easier to Ask for Forgiveness than Permission), meaning you let the function give an error and then "ask for forgiveness" (handle the error) as opposed to "asking for permission" (check in advance if there is a situation that will cause an error if the function is executed). Of course users are free to choose whatever works best for them.

Re: Love Error handling question

Posted: Sat Jan 14, 2023 4:15 pm
by ivan
I would go one step further than Pgimeno and ask: where are you loading your images from? Are the images located inside your .love file or project folder? You should definitely raise an error and block further execution if you game is not packaged correctly.
There is also: https://love2d.org/wiki/love.errorhandler