当前位置: 代码迷 >> Ruby/Rails >> ruby 错误处理:rescue
  详细解决方案

ruby 错误处理:rescue

热度:518   发布时间:2016-04-29 02:25:02.0
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");
if (file == NULL) {
fprintf( stderr, "File doesn't exist.\n" );
exit(1);
}
bytes_read = fread( buf, 1, bytes_desired, file );
if (bytes_read != bytes_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 return value
rescue
nil # Can't read the file? then don't return a string
end
end


有时我们会希望围绕问题展开创造性工作.这里,如果文件不存在,我们用标准输入代替:

begin
file = open("some_file")
rescue
file = STDIN
end
begin
# ... process the input ...
rescue
# ... and deal with any…………………………………………………………………………

原文:http://www.verydemo.com/demo_c119_i2013.html