On Thu, Feb 19, 2009 at 2:55 PM, Michal Suchanek <hramrach / centrum.cz> wrote:
> I think you are off the track here, at least WRT the original use of try.
>
> The #try(:name,...) would try to call :name on the object, and return
> nil (rather than an exception) when the object does not respond to the
> method (or in the case of Rails it does not have the requested
> attribute).
>
> The other thing I occasionally do and which could be more streamlined
> looks like this:
>
> here fd of a pipe might be already closed but it may not
>
>  fd.close rescue nil # hopefully prevents zombie hordes
>
> assume Object#quiet returns an object that does not raise an exception:
>
>  fd.quiet.close # hopefully prevent zombie hordes
>
> Here l is non-nil but it might not contain the field:
>
>  @src = l.scan( /src="(.*)"/)[0][0] rescue nil
>
> Not helping here - two [] calls have to be escaped, hard to read
>
>  @src = l.scan( /src="(.*)"/).quiet[0].quiet[0]
>
> However, the second quiet might not be necessary since the MatchData
> can be always dereferenced twice. Still not nice to read, though.
>
>  @src = l.scan( /src="(.*)"/).quiet[0][0]
>
> Here input might be a file or directory:
>
>  glob = Dir.glob input + File::Separator + "*" + $IN_EXT if
> (File.directory? input rescue nil)
>
> Much simpler condition here
>
>  glob = Dir.glob input + File::Separator + "*" + $IN_EXT if
> File.quiet.directory? input
>

It doesn't convince me. It can lead to bad programming style and could
hide important error from programmer (the same goes to "blah() rescue
nil" thing which should be used very very rarely). If programmer
decide to raise some error it does mean that it's imporatnt and
shouldn't be omitted. It also allow to continue when it shouldn't and
give possibility to propagate error

For example:
@src = l.scan( /src="(.*)"/).quiet[0][0]

How do you now if @src have "error" or good value? Ok, probably you
check in next line if it's nil or something but if you won't then
error can be propagated later.

Exceptions have concrete reason to be in language like Ruby and giving
more possibilities to omit them to programmer is bad idea.

PHP has something like '@'. It can be put before function name like:
file = @fopen(...)

It cause that php won't give any message about errors during execution
this function.
It sucks!

-- 
Pozdrawiam

Radosw Bu„Šat
http://radarek.jogger.pl - m blog