Jump to content




Coroutine error handling


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

#1 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 10 April 2015 - 10:21 PM

Hello guys, is there any way to discover what caused a coroutine to crash/die?

Thanks in advance

Creator

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 10 April 2015 - 10:28 PM

PIL said:

Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.

No, but this can be solved by wrapping your function in an error handler, something like this:

coroutine.create( function()
  local ok, err = pcall( func )
  if not ok then
    --#error handling here
  end
end )

Edited by KingofGamesYami, 10 April 2015 - 10:29 PM.


#3 wieselkatze

  • Members
  • 221 posts
  • LocationGermany

Posted 10 April 2015 - 10:34 PM

View PostKingofGamesYami, on 10 April 2015 - 10:28 PM, said:

PIL said:

Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.

No, but this can be solved by wrapping your function in an error handler, something like this:

That is not true - coroutine.resume() always returns a success value and a matching second value.
E.g. if you resume a coroutine it could return true, "redstone".
"redstone" in this case would be the filter caused by the coroutine.yield() in your coroutine.
If you use coroutine.yield("key") it'd return true, "key".

If now for whatever reason the coroutine fails to run - e.g. an error, the first value will be false and the matching value will be the error message.
E.g. if you run the following code:

function test()
pritn("hello") --# will error
end

local ok, res = coroutine.resume( coroutine.create( test ) )

print( ok .. ":" .. res )

This would print out the resume result and the error message caused.
"false:attempt to call nil" or sth. like that.

Edited by wieselkatze, 10 April 2015 - 10:36 PM.


#4 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 10 April 2015 - 10:42 PM

View PostKingofGamesYami, on 10 April 2015 - 10:28 PM, said:

PIL said:

Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.

No, but this can be solved by wrapping your function in an error handler, something like this:

coroutine.create( function()
  local ok, err = pcall( func )
  if not ok then
	--#error handling here
  end
end )

Thanks.

Did you get my message?

View Postwieselkatze, on 10 April 2015 - 10:34 PM, said:

View PostKingofGamesYami, on 10 April 2015 - 10:28 PM, said:

PIL said:

Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.

No, but this can be solved by wrapping your function in an error handler, something like this:

That is not true - coroutine.resume() always returns a success value and a matching second value.
E.g. if you resume a coroutine it could return true, "redstone".
"redstone" in this case would be the filter caused by the coroutine.yield() in your coroutine.
If you use coroutine.yield("key") it'd return true, "key".

If now for whatever reason the coroutine fails to run - e.g. an error, the first value will be false and the matching value will be the error message.
E.g. if you run the following code:

function test()
pritn("hello") --# will error
end

local ok, res = coroutine.resume( coroutine.create( test ) )

print( ok .. ":" .. res )

This would print out the resume result and the error message caused.
"false:attempt to call nil" or sth. like that.

Hey, thanks. Here, get a +1 and a potato. POTATO

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 10 April 2015 - 11:11 PM

View PostKingofGamesYami, on 10 April 2015 - 10:28 PM, said:

PIL said:

Note that resume runs in protected mode. Therefore, if there is any error inside a coroutine, Lua will not show the error message, but instead will return it to the resume call.

No, but this can be solved by wrapping your function in an error handler, something like this:

coroutine.create( function()
  local ok, err = pcall( func )
  if not ok then
    --#error handling here
  end
end )

Did you read the paragraph in the PIL you quoted? You should read through the implementation of pcall in the ComputerCraft bios.

#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 11 April 2015 - 12:45 AM

Well this is embarrassing, especially considering that's how I thought it worked - I looked it up in the PIL to make sure. *facepalm*





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users