我有這個代碼,可以在生產中觸發「非本地出口檢測!」科。我無法理解如何發生,因爲即使返回也會觸發NonLocalExit異常。即使拋出也會引發異常。從紅寶石檢測和處理NonLocalExit
有什麼辦法可以讓exception_raised和yield_returned都是假的?
def transaction
yield_returned = exception_raised = nil
begin
if block_given?
result = yield
yield_returned = true
puts 'yield returned!'
result
end
rescue Exception => exc
exception_raised = exc
ensure
if block_given?
unless yield_returned or exception_raised
puts 'Non local exit detected!'
end
end
end
end
transaction do
puts 'good block!'
end
transaction do
puts 'starting transaction with block with return'
return
puts 'this will not show'
end
輸出:
good block!
yield returned!
starting transaction with block with return
我想以某種方式輸出 '檢測非本地退出!'。我知道這發生在生產中,但我無法在開發中實現。試着用一個回報和一個投擲,但他們都提出了一個例外。任何其他方式?
這是我想了解遺留代碼。我需要了解它是如何達到'非本地出口檢測!' – Costi
那麼,現在唯一發生在我身上的是'callcc',這看起來很牽強。你能夠改變代碼嗎?添加一個參數'&block'並用'block.source_location'檢查確保塊內的原點。 –