Ruby does not 'ensure' when I 'retry' in 'rescue'

后端 未结 2 603
逝去的感伤
逝去的感伤 2020-12-31 12:48

Consider this begin-rescue-ensure block:

attempts=0
begin
  make_service_call()
rescue Exception
  retry unless attempts>2
  exit -1
ensure
  attemps += 1         


        
2条回答
  •  长情又很酷
    2020-12-31 13:12

    ensure code is executed once, just before the code block exits and it will be called at that time.

    But because of the unless attempts>2 condition, and the fact that ensure will only be called "just before the code exits" (e.g. due to exit -1) the attempts += 1 will not be executed, and so there is an infinite loop.

    ensure is like __finally in C++ : you could catch the exception and then use a goto : but finally will not be called until the function is actually about to exit.

提交回复
热议问题