From: Robert Klemme Date: 2008-06-14T18:44:21+09:00 Subject: Re: raise and rescue On 14.06.2008 00:31, Michael W. Ryder wrote: > Robert Klemme wrote: >> There is no way - and for good reason. An exception is thrown because >> execution cannot proceed at that line. > > So how would you do something that requires user intervention, say in > putting more paper in a printer? A lot of my programs require files or > other resources that may not always be available and I have programmed > them so that an error message is displayed and the operator can fix the > problem and continue or abort the program. That's exactly the case where /retry/ is meant for. Catch the exception, do something and then retry the block of code (see the example I posted earlier). > A very good example is when > a user tries to bring up an account that is in use by another user. The > program states that the record is busy and the operator can wait for the > record to be freed and continue or go to another account. > I can see how to do this in Ruby by using something like a while loop > but that requires that the code surround every possible trouble spot > rather than having a "generic" exception handling routine like I > currently use in Business Basic. See above. >> There is however retry, but it will continue from the beginning of the >> block: >> >> irb(main):001:0> i = 0 >> => 0 >> irb(main):002:0> begin >> irb(main):003:1* puts 1 >> irb(main):004:1> raise "Foo" >> irb(main):005:1> rescue Exception >> irb(main):006:1> puts 2 >> irb(main):007:1> i += 1 >> irb(main):008:1> retry if i < 3 >> irb(main):009:1> end >> 1 >> 2 >> 1 >> 2 >> 1 >> 2 >> => nil >> irb(main):010:0> Cheers robert