闲云无衣
无衣的笔记
异常(exception)本身也是对象,类型是Exception或是其子类。raise方法会抛出一个异常,这会中断代码的正常运转流程,Ruby会到调用栈中寻找能够处理该异常的代码。

如果要处理异常,可将代码放在begin和end关键字之间,并用ruscue子句来拦截特定类型的异常。

begin
   ....
rescue BlogDat6aNotFound
   ...
rescue Exception =>exc
   ...
ensure
   ... #无论begin块是否成功,ensure代码域都将执行
end
posted on 2007-02-08 14:47 无衣 阅读(323) 评论(0)  编辑  收藏 所属分类: rails