Is an exception (Error) raised while using YAML.load_file()


  Well, I am saving db properties in a YAML file in a ruby

application (not Rails) and was wondering if an exception is raised if
the file specified is not found. If so which is error. I want to handle
the uncertainty in the availability of the file with rescue.


Actually let me improve my question. This is the erro I get

C:/Ruby/lib/ruby/1.8/yaml.rb:143:in initialize': No such file or directory - file2.yaml (Errno::ENOENT) from C:/Ruby/lib/ruby/1.8/yaml.rb:143:inopen’
from C:/Ruby/lib/ruby/1.8/yaml.rb:143:in `load_file’
from yaml_prac.rb:3

doesn’t make any sense.


Errno::ENOENT is a system error, which means “No such file or
These values are Constants defined in Errno, so you can see the possible
values via Errno.constansts.
With that piece of information, the error that you get means that in
yaml.rb, on line 143, you were trying to open the file “file2.yaml”
did not exist, probably because it is not in the current working
but maybe because you didn’t ever create it, or because you named it
“FILE2.YAML” and you have a case-sensitive file system.


On Sat, Jul 25, 2009 at 12:19 AM, Venkat A. <

Thanks for the reply Ray

      I purposefully did not create the file to see what error will 

be raised. I am sorry I failed to mention that. I am trying to ensure
that this error will not be thrown to the user when a file is not found.
Hence I want to handle it with rescue. How can I achieve this.


The usual way,

?>“fjaskfajs”) # Or, whatever code you have on line 3 of

rescue Errno::ENOENT
p “file not found”
“file not found”
=> nil


On Sat, Jul 25, 2009 at 10:37 AM, Venkat A. <

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs