From: David Vallner Date: 2006-02-11T04:52:39+09:00 Subject: Re: Yet another question on exceptions Dňa Piatok 10 Február 2006 19:43 Eric Jacoboni napísal: > David Vallner writes: > > Well, File.open in the block version will reraise the exception precisely > > to let you do that, it's not supposed to silently ignore errors. > > Ok, perhaps i'm not clear or perhaps i've missed something... > > Suppose i want my script exit with different values depending on the > error cases: > > % ruby -e 'File.open("no.txt") {|f| line = f.gets}' > -e:1:in `initialize': No such file or directory - no.txt (Errno::ENOENT) > from -e:1 > % echo $? > 1 > % ruby -e 'File.open("yes.txt") {|f| f.puts("bla")}' > -e:1:in `write': not opened for writing (IOError) > from -e:1 > from -e:1 > [titine]:~/Desktop % echo $? > 1 > > As it's clearly not the same error, i don't want the same return > status. As i don't know how to manage this gracefully with Ruby, i'm > using a well-know idiom (at least for me...). That said, if there a > more rubywaying solution, i buy it. If you want to assign your own error codes, feel free to do so. As I said, that's why File will reraise the exception. What I wanted to emphasise is that you don't need to take care of cleanup of file handles while still being able to handle errors as you want to. There's nothing wrong with what you wrote in the original post, as long as you keep to printing the error message to standard error to keep with conventions.