Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
handling errors — Gideros Forum

handling errors

ricvieiraricvieira Member
edited October 2013 in General questions
Hi, I have another noob question, I've been trying to understand how to handle errors in gideros, in java we use try/catch blocks to encapsulate what we want to test and deal with the error, in lua I found the pcall function, but it doesnt seem to work that well

if I do this, the pcall works well
function func(n)
    return 'N'+n;
end 
 
 if pcall(func, n) then
 print("worked")
 else
 print("Error")
 end
 
--->Error
but when I try to use it with gideros functions it doesnt work:
mychild =  Bitmap.new(Texture.new("images/image1.png"))
 if pcall(self:removeChild(), mychild) then
 print("worked")
 else
 print("Error")
 end
 
--->start.lua:88: bad argument #1 to 'removeChild' (Sprite expected, got no value)
or if I do like this:
mychild = Bitmap.new(Texture.new("images/image1.png"))
 if pcall(self:removeChild(mychild)) then
 print("worked")
 else
 print("Error")
 end
 
 
--->start.lua:88: The supplied Sprite must be a child of the caller.
So is there a way to use some kind of try/catch block in gideros, besides pcall, that I can check if a variable is a child of self, or using another self function? or am I using pcall wrong??

tnx in advance

Likes: Yan

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited October 2013 Accepted Answer
    it seems like incorrect usage, can you try this one:
    mychild =  Bitmap.new(Texture.new("images/image1.png"))
    if pcall(self.removeChild, self, mychild) then
     print("worked")
    else
     print("Error")
    end
    +1 -1 (+1 / -0 )Share on Facebook
  • Accepted Answer
    @ricvieira
    If you want to use pcall, change to this
    mychild = Bitmap.new(Texture.new("images/image1.png"))
    if pcall(self.removeChild,self,mychild) then
     print("worked")
     else
     print("Error")
     end
    Or you can use this method
    mychild:removeFromParent()
    Coming soon
  • @ar2rsawseen super fast :D, when i google and try the code, you have the answer :D
    Coming soon
  • tnx ar2rsawseen and vitalitymobile

    I had tried with both self and mychild has arguments, but I always tried with the "self:removeChild" and not with just the .

    but I now read the difference between the two calls, when I use . Im calling the function and passing self has first parameter.

    I know about removeFromParent(), Im using that, this was just trying to understand how to handle run time errors with gideros, it wasnt about the removeChild function itself ;) Im still learning :D

    tnx again, you guys rock :D

    +1 -1 (+1 / -0 )Share on Facebook
  • OZAppsOZApps Guru
    Accepted Answer
    @ricvieira,
    not jumping into the quick draw contest here, for understanding a few things

    1. the difference between : and .
    object:func()
    ==> is the same as
    object.func(object)
    2. about pcall,
    there is a difference between
    func
    and
    func()
    where
    type(func)
    is "function" and
    type(func())
    is the return type of the function. pcall invokes the function and the parameters, so if you pass it func() then it will not work as you are calling the return value as a function which will not work as it is not a function.

    Hope that helps you understand pcall and the difference between . and :
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
    +1 -1 (+2 / -0 )Share on Facebook
  • yeah, it helps a lot, tnx OZApps, I know from experience that error handling is a really important part of every project, thats why I wanted to understand it better how to do it here in lua/gideros

    tnx again, you guys rock, Im loving gideros, not just for the tech itself, but cause of this awesome community, always willing to help and answering noob questions like mine :D
Sign In or Register to comment.