[LRUG] Exceptions as a flow control mechanism

David Salgado david at digitalronin.com
Wed Dec 19 04:32:38 PST 2007


I agree with the (more or less) consensus that came out the last time
we discussed this, that exceptions should not be part of the normal
control flow of your application.

> Which is, to summarise, advocating throwing an exception when, say,
> you can't find a particular record with that ID, and then letting some
> Rails magic pick up the pieces and run a method accordingly.
>
> I'd always thought that exceptions shouldn't really be used for this,
> although I don't have a particularly solid justification for this
> belief. Am I wrong? What do you think?

In the example, there is a URL with an ID at the end, and you get an
error if there is no such record. The new rails trick is used to show
a nicer error page for that particular exception - i.e. if someone
follows an out-of-date link, or if they're messing with the ID value
by hand to fish around your application looking for records they
shouldn't be able to see.

Personally, I don't have a problem with that. But, if the same trick
was used to implement some kind of "find or create" control flow, and
present a data input form instead of an error page, I think that would
be a bit dubious.

Just my 2c.

David



More information about the Chat mailing list