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
Love Error handling question
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Love Error handling question
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
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
pgimeno : Thank you, thats exactly what i was looking for
Re: Love Error handling question
Debug without errors will be much harder
Re: Love Error handling question
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().
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.
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 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
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
There is also: https://love2d.org/wiki/love.errorhandler
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 10 guests