rubyonrails:ruby 异常处理:rescue

个运行着常会遇到意外问题.个要读取文件不存在;当希望存入些数据时磁盘满了;用户可能输入不恰当数据.
ruby> file = open("some_file")
ERR: (eval):1:in `open': No such file or directory - some_file

个健壮会合理并漂亮处理这些问题.面对那些异常是件讨人厌工作.C员被要求做到检查每个可能导致发生系统返回值并立刻做出决定.
FILE *file = fopen("some_file", "r");
(file NULL) {
fprf( stderr, "File doesn't exist.\n" );
exit(1);
}
s_read = fread( buf, 1, s_desired, file );
(s_read != s_desired ) {
/* do more error handling here ... */
}
...

这项无聊工作会使员最终变得马虎并忽略掉它,结果是无法应对异常.令方面,这样也会降低可读性.过多处理使有意义代码也变得杂乱了.
在Ruby里,就像其它现代语言,我们可以通过隔离办法处理代码域里异常,因此,这有着惊人效果却又不会为员或以后希望读它其它人造成过度负担.代码域由begin开始直到遇到个异常,这将导致转向个由rescue标记处理代码域.如果异常没发生,rescue代码就不会使用.下面代码返回文本文件行,如果有异常则返回 nil.
def first_line( filename )
begin
file = open("some_file")
info = file.gets
file.close
info # Last thing evaluated is the value
rescue
nil # Can't read the file? then don't a
end
end

有时我们会希望围绕问题展开创造性工作.这里,如果文件不存在,我们用标准输入代替:
begin
file = open("some_file")
rescue
file = STDIN
end
begin
# ... process the input ...
rescue
# ... and deal with any other exceptions here.
end

retry 用于 rescue 代码表示又重新执行 begin 代码.这让我们可以压缩前面例子:
fname = "some_file"
begin
file = open(fname)
# ... process the input ...
rescue
fname = "STDIN"
retry
end

但这仍有点瑕疵.个不存在文件将导致不停止地 retry.你在使用 retry 做异常处理时应注意到这点.
每个Ruby库在遇到时都会提交个异常,你可以在自己代码里明确地提交异常.用 raise 来提交异常.它带个参数,也就是描述异常串.参数是可选但不应被省略.的后它可以通过个特殊全局变量 $! 访问.
ruby> raise "test error"
test error
ruby> begin
| raise "test2"
| rescue
| pr "An error occurred: ",$!, "\n"
| end
An error occurred: test2
nil
Tags:  ruby是什么 ruby教程 ruby是什么意思 rubyonrails

延伸阅读

最新评论

发表评论