ruby 异常处理:rescue

时间:2022-06-01 19:54:28

一个运行着的程序常会遇到意外的问题.一个要读取的文件不存在;当希望存入一些数据时磁盘满了;用户可能输入不恰当的数据.

ruby>file=open("some_file")

ERR:(eval):1:in`open':Nosuchfileordirectory-some_file

一个健壮的程序会合理并漂亮的处理这些问题.面对那些异常是一件讨人厌的工作.C程序员被要求做到检查每一个可能导致错误发生的系统调用的返回值并立刻做出决定.

FILE*file=fopen("some_file","r");

if(file==NULL){

fprintf(stderr,"Filedoesn'texist.\n");

exit(1);

}

bytes_read=fread(buf,1,bytes_desired,file);

if(bytes_read!=bytes_desired){

/*domoreerrorhandlinghere...*/

}

...

这项无聊的工作会使程序员最终变得马虎并忽略掉它,结果是程序无法应对异常.令一方面,这样也会降低程序的可读性.因为过多的错误处理使有意义的代码也变得杂乱了.

在Ruby里,就像其它的现代语言,我们可以通过隔离的办法处理代码域里的异常,因此,这有着惊人的效果却又不会为程序员或以后希望读它的其它人造成过度的负担.代码域由begin开始直到遇到一个异常,这将导致转向一个由rescue标记的错误处理代码域.如果异常没发生,rescue代码就不会使用.下面的代码返回文本文件的第一行,如果有异常则返回nil.

deffirst_line(filename)

begin

file=open("some_file")

info=file.gets

file.close

info#Lastthingevaluatedisthereturnvalue

rescue

nil#Can'treadthefile?thendon'treturnastring

end

end

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

begin

file=open("some_file")

rescue

file=STDIN

end

begin

#...processtheinput...

rescue

#...anddealwithanyotherexceptionshere.

end

retry用于rescue代码表示又重新执行begin代码.这让我们可以压缩前面的例子:

fname="some_file"

begin

file=open(fname)

#...processtheinput...

rescue

fname="STDIN"

retry

end

但这仍有一点瑕疵.一个不存在的文件将导致不停止地retry.你在使用retry做异常处理时应注意到这一点.

每个Ruby库在遇到错误时都会提交一个异常,你可以在自己的代码里明确地提交异常.用raise来提交异常.它带一个参数,也就是描述异常的一个字符串.参数是可选的但不应被省略.之后它可以通过一个特殊的全局变量$!访问.

ruby>raise"testerror"

testerror

ruby>begin

|raise"test2"

|rescue

|print"Anerroroccurred:",$!,"\n"

|end

Anerroroccurred:test2

nil