2010-03-17 41 views
38

我試圖從一個「需求」中解救出來:沒有這樣的文件要加載到ruby中,以 的順序提示用戶指定-I標誌以防萬一他忘記了。 基本上代碼如下:如何從'require'中解救:沒有這樣的文件要加載到ruby中?

begin 
    require 'someFile.rb' 
rescue 
    puts "someFile.rb was not found, have you" 
    puts "forgotten to specify the -I flag?" 
    exit 
end 

我期待有rescue部分接管執行的情況下,someFile.rb沒有被發現,但我的假設是錯誤的。

回答

47

你必須明確地定義你想從中解救出哪個錯誤。

begin 
    require 'someFile.rb' 
rescue LoadError 
    puts "someFile.rb was not found, have you" 
    puts "forgotten to specify the -I flag?" 
    exit 
end 
相關問題