打印

RUBY文档中心中关于异常处理:ensure这一节的一个问题

RUBY文档中心中关于异常处理:ensure这一节的一个问题

看最后一段代码

begin
  file = open("/tmp/some_file", "w")
  # ... write to the file ...
rescue
  # ... handle the exceptions ...
ensure
  file.close   # ... and this always happens.
end 


ensure的异常处理条件是,无论如何,这个异常一定要处理,那么,他在这里加上了file.close,如果在file.open的时候出现问题,或者是找不到文件呀之类的异常条件,那么open方法就没有执行,那又何来的close呢?这不是错上加错吗?

TOP

TOP

2008-12-05 05:05 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html) @38.103.63.61