Jump to content




[Lua] Catching an error


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

#1 anonimo182

  • Members
  • 252 posts
  • LocationIn the universe

Posted 19 December 2012 - 03:45 PM

There is a way to catch the error a program gets? Like : bios [string "Test"] "=2 expected
I already tried pcall, but when a program fails, it only returns false and true, which is not what I expect

Thanks

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 December 2012 - 03:59 PM

pcall should be returning a second argument in those cases, which would contain the error message. Perhaps you need assert instead, I can't really recall. Either way, look at the second return value.

#3 anonimo182

  • Members
  • 252 posts
  • LocationIn the universe

Posted 19 December 2012 - 04:02 PM

 Lyqyd, on 19 December 2012 - 03:59 PM, said:

pcall should be returning a second argument in those cases, which would contain the error message. Perhaps you need assert instead, I can't really recall. Either way, look at the second return value.
that value always give me true, no matter what

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 December 2012 - 04:05 PM

Clearly, it doesn't:

loadfile = function( _sFile )
    local file = fs.open( _sFile, "r" )
    if file then
        local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
        file.close()
        return func, err
    end
    return nil, "File not found"
end

dofile = function( _sFile )
    local fnFile, e = loadfile( _sFile )
    if fnFile then
        setfenv( fnFile, getfenv(2) )
        return fnFile()
    else
        error( e, 2 )
    end
end

-- Install the rest of the OS api
function os.run( _tEnv, _sPath, ... )
    local tArgs = { ... }
    local fnFile, err = loadfile( _sPath )
    if fnFile then
	    local tEnv = _tEnv
	    --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
        setmetatable( tEnv, { __index = _G } )
	    setfenv( fnFile, tEnv )
	    local ok, err = pcall( function()
	        fnFile( unpack( tArgs ) )
	    end )
	    if not ok then
	        if err and err ~= "" then
   	         printError( err )
   		 end
	        return false
	    end
	    return true
    end
    if err and err ~= "" then
        printError( err )
    end
    return false
end

That's from bios.lua, from ComputerCraft.

#5 ChunLing

  • Members
  • 2,027 posts

Posted 19 December 2012 - 04:09 PM

You need to use multiple assignment to catch the second return. Like:
rone,rtwo = pcall(func)

That corresponds to the
local ok, err = pcall(function() fnFile(unpack(tArgs)) end ) 
part of the bios, but is a little less complicated and probably better suited to your needs.

Edited by ChunLing, 19 December 2012 - 04:12 PM.


#6 anonimo182

  • Members
  • 252 posts
  • LocationIn the universe

Posted 19 December 2012 - 04:15 PM

 ChunLing, on 19 December 2012 - 04:09 PM, said:

You need to use multiple assignment to catch the second return. Like:
rone,rtwo = pcall(func)

That corresponds to the
local ok, err = pcall(function() fnFile(unpack(tArgs)) end ) 
part of the bios, but is a little less complicated and probably better suited to your needs.
I am using that, like this:
ok, err = pcall(os.ru({}, program)
but the second variable always return true

Or I need to use only os.run?

#7 ChunLing

  • Members
  • 2,027 posts

Posted 19 December 2012 - 04:34 PM

...you need to not use os.run(), that prevents error propagation but it doesn't return an error message. Just use "ok,err = pcall(program)"

#8 anonimo182

  • Members
  • 252 posts
  • LocationIn the universe

Posted 19 December 2012 - 04:37 PM

 ChunLing, on 19 December 2012 - 04:34 PM, said:

...you need to not use os.run(), that prevents error propagation but it doesn't return an error message. Just use "ok,err = pcall(program)"
Like:
ok, err = pcall(myProgram, my argument, soOn)
If yes, thank you! If not, please explain me how

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 December 2012 - 06:05 PM

Yes, with the caveat that myProgram will need to be whatever loadfile (or loadstring on the contents of the file) returned.

#10 ChunLing

  • Members
  • 2,027 posts

Posted 19 December 2012 - 08:56 PM

Hmmm...how to pass the command line parameters, though? Concatenate them onto the front of the string to load?

#11 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 20 December 2012 - 03:40 AM

No, you pass arguments just like he has them on this latest example. Well, without spaces in variable names, but you know what I mean.

#12 ChunLing

  • Members
  • 2,027 posts

Posted 20 December 2012 - 05:38 PM

Oh, right, cause you pass them to pcall and not to loadstring. I derped out there.

#13 anonimo182

  • Members
  • 252 posts
  • LocationIn the universe

Posted 21 December 2012 - 08:36 AM

Well after trying, it always give me the same error:
atempt to call string
even that I am using
pcall("pahtoprogram/program", args)

Edit: Fixed it





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users