一個運行著的程序常會遇到意外的問題.一個要讀取的文件不存在;當希望存入一些數據時磁盤滿了;用戶可能輸入不恰當的數據.
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 ...