From: Dave Thomas Date: 2001-08-08T07:28:43+09:00 Subject: [ruby-talk:19314] Re: how to "die" in Ruby markus jais writes: > hi, > in Perl I can write > open(FH, "/root/.bashrc") or die "error opening file: $!"; > > my ruby solution looks like this: > > begin > f = File.new("/root/.bashrc") > f.each { |line| > print line if line =~ /blue/ > } > rescue Errno::ENOENT > print "no such file or directory\n" > rescue Errno::EACCES > print "permission denied\n" > end I think I'd be tempted to restructure just slightly to make the error handling reusable. Perhaps something like def file_error(ex) case ex when Errno::ENOENT then puts "No such file or directory" when Errno::EACCES then puts "Permission denied" else puts $! end end File.open(ARGV[0]) do |file| file.each do |line| print line if line =~ /blue/ end end rescue file_error($!) Then, for this particular example, I'd use the fact the File is enumerable (heh - twice in two days :) puts File.open(ARGV[0]).grep(/blue/) rescue file_error($!) Of course, to answer your original question, in Ruby you could write > open(FH, "/root/.bashrc") or die "error opening file: $!"; as f = File.open("/root/.bashrc") rescue raise "Error opening file: $!" Or you could just leave of the rescue clause altogether and let the error propagate up naturally. Regards Dave