ruby 异常处理:rescue
            脚本专栏 发布日期:2025/11/4 浏览次数:1
         
        
            正在浏览: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 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 
    |   print "An error occurred: ",$!, "\n" 
    | end 
An error occurred: test2 
   nil