From: tamouse mailing lists Date: 2012-11-17T12:47:06+09:00 Subject: Re: Question on exceptions On Fri, Nov 16, 2012 at 6:27 AM, Robert Klemme wrote: > On Fri, Nov 16, 2012 at 9:27 AM, tamouse mailing lists > wrote: > >> Is this a good way to use exceptions? Having an insufficent balance >> might be something the user finds exceptional, but I'd think it would >> be a standard sort of handling for a bank applicaion... > > Depends on how you implement it: I know the right answer to every question is "It depends" :) > if you have the option to query the > current balance and define that a withraw operation is only allowed > with sufficient funds then you throw an exception. If OTOH you define > that withdraw will return success then you have a boolean return > value. Both approaches are legal and feasible. I guess I'm looking for some kind of guidance to generalize this. My understanding is that one only uses exceptions for things which the app finds exceptional, meaning things which it cannot or should not be dealing with, which in some cases may depend on the business practices and policies one is implementing. Recognizing this was a class assignment for the OP, the situation is highly contrived, and the lesson might be how to use and handle exceptions, so great. Do you (or anyone else who'd like to chime in!) have a ... set of heuristics, maybe? .. that help you know when it's appropriate and when it's not? The reason I ask is not merely academic. I see in code many places where it is raising and exception, and then the rescue code does something like this: rescue Exception # blah blah end In other words, they know an exception might be raised, but they trap evey possible exception, rather than a specific one. I'm worried (?) (thinking) that use of raising exceptions might be a little too easily decided upon as the answer, and if I should maybe push back on such implementations... > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > Thanks, so much, Robert. :) And I am going to check out your web site right now.