Love Error handling question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
elfrea
Prole
Posts: 8
Joined: Fri Jan 06, 2023 11:31 pm

Love Error handling question

Post 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
User avatar
darkfrei
Party member
Posts: 1216
Joined: Sat Feb 08, 2020 11:09 pm

Re: Love Error handling question

Post by darkfrei »

:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

Re: Love Error handling question

Post 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
elfrea
Prole
Posts: 8
Joined: Fri Jan 06, 2023 11:31 pm

Re: Love Error handling question

Post 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
User avatar
darkfrei
Party member
Posts: 1216
Joined: Sat Feb 08, 2020 11:09 pm

Re: Love Error handling question

Post by darkfrei »

Debug without errors will be much harder :)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

Re: Love Error handling question

Post 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.
User avatar
ivan
Party member
Posts: 1924
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Love Error handling question

Post 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
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests