print('-- protected error in coroutine (test 1) --')
coro = coroutine.create(function()
	print('inside coro')
	print((pcall(function()
		print('throwing error')
		error(233)
		print('should never reach here')
	end)))
	print('inside coro after pcall')
	return "return value from coro"
end)
print('starting coro')
print(coroutine.resume(coro))
print(coroutine.status(coro))

print('-- protected error in coroutine (test 2) --')
coro = coroutine.create(function()
	print('inside coro')
	print((xpcall(function()
		print('throwing error')
		error(false)
		print('should never reach here')
	end, function(err)
		print('inside error handler, err=', err)
		return "return value from error handler"
	end)))
	print('inside coro after pcall')
	return "return value from coro"
end)
print('starting coro')
print(coroutine.resume(coro))
print(coroutine.status(coro))

print('-- protected error in coroutine (test 3) --')
coro = coroutine.create(function()
	print('inside coro')
	print((pcall(function()
		print('throwing error')
		error(233)
		print('should never reach here')
	end)))
	print('inside coro after pcall 1')
	coroutine.yield("yield value from coro")
	print('inside coro after yield')
	print((pcall(function()
		print('throwing error after yield')
		error(233)
		print('should never reach here')
	end)))
	print('inside coro after pcall 2')
	return "return value from coro"
end)
print('starting coro')
print(coroutine.resume(coro))
print(coroutine.status(coro))
print(coroutine.resume(coro))
print(coroutine.status(coro))

print('-- protected error in coroutine (test 4) --')
coro = coroutine.create(function()
	print('inside coro')
	print((xpcall(function()
		print('throwing error')
		error(false)
		print('should never reach here')
	end, function(err)
		print('inside error handler, err=', err)
		return "return value from error handler"
	end)))
	print('inside coro after pcall')
	coroutine.yield("yield value from coro")
	print('inside coro after yield')
	print((xpcall(function()
		print('throwing error after yield')
		error(false)
		print('should never reach here')
	end, function(err)
		print('inside error handler after yield, err=', err)
		return "return value from error handler"
	end)))
	print('inside coro after pcall')
	return "return value from coro"
end)
print('starting coro')
print(coroutine.resume(coro))
print(coroutine.status(coro))
print(coroutine.resume(coro))
print(coroutine.status(coro))


