[LRUG] Exceptions when to throw

Anthony Green Anthony.Green at bbc.co.uk
Wed Dec 19 04:35:52 PST 2007


Jim Weirich did a presentation on Design by Contract at Ruby Hoedown that is
still available as a podcast.

It had to be cut sort by he managed to mention briefly how throwing an
exception was linked to a failure of the object to fulfill its contract.

Best

Tony

On 19/12/07 12:22, "Tim Harding" <tim.harding at gmail.com> wrote:

> I guess it all comes down to how you define exceptional.
> 
> The argument put forward for find(params[:id]) throwing an exception in the
> first place is that if you're looking for a particular record and it isn't
> there then you probably had good reason to expect it to exist, there was a
> link to it or whatever. It would be pretty surprising if your app linked to a
> record that didn't exist. That's exceptional according to the Rails framework.
> 
> If you try to find(:all) then you're looking for a set and an empty set is a
> normal kinda set, returning an empty collection is unexceptional.
> 
> They're basically equating record not found to page not found here.
> 
> Perhaps it all comes down to how your app is specified to handle record not
> found, if there's something actually useful you can do on RecordNotFound that
> will help the user then handle the exception where it is raised. If there are
> reasonable decisions you can take in each different instance then global
> exception handling isn't very useful.
> 
> If you're working on a CMS and an :id actually represents a page then being
> able to declare what should happen to RecordNotFound, i.e., presenting a 404
> is actually remarkably useful and saves a whole lot of worry about letting
> things slip through the gap.
> 
> What do you do to handle a record not found in your app?
> 
> 
> On Dec 19, 2007 4:50 AM, James Adam < james.adam at gmail.com> wrote:
>> I think we've talked about this before, but Rails 2.0 has taken a
>> stance so I thought it might be interesting to pick the "Best Minds In
>> London" (i.e. you filthy lot) about the use of exception handling as a
>> form of flow control.
>> 
>> I'm talking about this:
>> http://almosteffortless.com/2007/10/08/graceful-404s-in-rails-20/
>> 
>> 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?
>> 
>> Actually, here's the quote, and I take issue with a different aspect
>> of it here too:
>> 
>>  "Lots of common exceptions would do better to be rescued at a shared
>> level rather than per action. This has always been possible by
>> overwriting rescue_action_in_public, but then you had to roll out your
>> own case statement and call super. Bah. So now we have a class level
>> macro called rescue_from, which you can use to declaratively point
>> certain exceptions to a given action."
>> 
>> Surely "rescue_action_in_public" was only there to handle unforseen
>> catastrophic errors? It seems like massive overkill/folly for anyone
>> to try and use this to handle ActiveRecord::RecordNotFound, right?
>> 
>> --
>> * J *
>>   ~
>> _______________________________________________
>> Chat mailing list
>> Chat at lists.lrug.org
>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> 
> 
> 
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org


-- 
Anthony Green
Client Side Developer
BBC Future Media Technology - Audio & Music
Room 718 | 7th Floor | Henry Wood House
* 020 776 (51602) | 07941 253 272


http://www.bbc.co.uk/
This e-mail (and any attachments) is confidential and may contain personal views which are not the views of the BBC unless specifically stated.
If you have received it in error, please delete it from your system.
Do not use, copy or disclose the information in any way nor act in reliance on it and notify the sender immediately.
Please note that the BBC monitors e-mails sent or received.
Further communication will signify your consent to this.
					
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20071219/291d1bae/attachment.html>


More information about the Chat mailing list