Ren*_*ger 42 ruby exception-handling require
我试图从``require'中解救:没有这样的文件加载到ruby`中以暗示用户指定-I标志以防他忘记这样做.基本上代码看起来像:
begin
require 'someFile.rb'
rescue
puts "someFile.rb was not found, have you"
puts "forgotten to specify the -I flag?"
exit
end
Run Code Online (Sandbox Code Playgroud)
我原本预计该rescue
部分会在someFile.rb
未找到的情况下接管执行,但我的假设是错误的.
sev*_*rin 56
抢救不带参数只救StandardError的秒.该LoadError(由找不到文件提出的)是不是StandardError的,但一个ScriptError(见http://blog.nicksieger.com/articles/2006/09/06/rubys-exception-hierarchy).因此,您必须明确地拯救LoadError,如MBO所示.
MBO*_*MBO 50
您必须明确定义要从中拯救的错误.
begin
require 'someFile.rb'
rescue LoadError
puts "someFile.rb was not found, have you"
puts "forgotten to specify the -I flag?"
exit
end
Run Code Online (Sandbox Code Playgroud)