← Back to Lua Course | Chapter 7: Coroutines | Lesson 7 of 7

Errors और Coroutine Helpers

एक coroutine के अंदर errors resume से catch होते हैं, और helper functions बताते हैं कि आप कहाँ हैं।

Errors और Coroutine Helpers

अगर एक coroutine एक error raise करे, coroutine.resume false और error message return करता है और coroutine dead बन जाता है। coroutine.running current coroutine और एक boolean return करता है जो main thread के लिए true होता है। coroutine.isyieldable बताता है कि current code yield कर सकता है या नहीं, और Lua 5.3 में एक coroutine एक pcall के आर-पार yield कर सकता है।

Note: main program खुद एक coroutine है, लेकिन यह yield नहीं कर सकता।

उदाहरण: Errors and coroutine helpers

lua
local co = coroutine.create(function()
  error("oops", 0)
end)
print(coroutine.resume(co))
print(coroutine.status(co))

print(select(2, coroutine.running()))
print(coroutine.isyieldable())
coroutine.wrap(function()
  print(coroutine.isyieldable(), select(2, coroutine.running()))
  local ok, v = pcall(function() return coroutine.yield("across pcall") end)
  print(ok, v)
end)()

-- Output:
-- false	oops
-- dead
-- true
-- false
-- true	false
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि एक coroutine के अंदर एक error पूरे program को crash कर देगा
  2. error होने के बाद एक coroutine resume करना
  3. main thread से yield करने की कोशिश करना
चैप्टर सारांश
  • resume errors catch करता है
  • error होने वाला coroutine dead है
  • coroutine.running current वाला identify करता है
  • isyieldable check करता है कि yield allowed है या नहीं
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.