作者tonytonyjan (南洋大兜虫)
看板Ruby
标题[问题] rescue里头的else clause行为
时间Tue Aug 2 12:43:30 2011
今天在Ruby Forum看到一篇讨论串
探讨The Ruby Way这本书对於else的解释
原网址:
http://www.ruby-forum.com/topic/90997
书:
http://www.informit.com/store/product.aspx?isbn=0672328844
在书里头的节录:
In the event that error types are specified, it may be that an
exception does not match any of these types. For that situation, we are
allowed to use an else clause after all the rescue clauses.
意思是如果值出的异常我们都没有救到的话(rescue),则会执行else clause
这个解释我也曾在书上看过
然而实验的证明
else clause只会在"没有任何异常掷出"时才会执行
意思是下面这两个写法结果是一样的:
----------
begin
# Nothing Happens
rescue
else
puts "Do something if no exceptoin was throwed."
end
----------
begin
# Nothing Happens
rescue
end
puts "Do something if no exceptin was throwed."
----------
问题:
1. 这两种写法既然结果一样,但有没有什麽理由能让我们从中选择一个比较好的写法?
否则else感觉没有什麽大用
2. 会有else行为的争议,是因为Ruby版本的问题吗?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.235.115