Hello guys, is there any way to discover what caused a coroutine to crash/die?
Thanks in advance
Creator
Coroutine error handling
Started by Creator, Apr 10 2015 10:21 PM
5 replies to this topic
#1
Posted 10 April 2015 - 10:21 PM
#2
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
Posted 10 April 2015 - 10:34 PM
KingofGamesYami, 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
Posted 10 April 2015 - 10:42 PM
KingofGamesYami, 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?
wieselkatze, on 10 April 2015 - 10:34 PM, said:
KingofGamesYami, 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
Posted 10 April 2015 - 11:11 PM
KingofGamesYami, 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
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











