From: "Michael W. Ryder" <_mwryder@...> Date: 2008-06-14T07:34:32+09:00 Subject: Re: raise and rescue Robert Klemme wrote: > On 13.06.2008 21:58, Misiek Sz wrote: >> Is is possible to raise an exception then rescue it and then go back to >> whereever it was raised and continue with execution? >> For example, >> class Test >> end >> >> begin >> code line 1 >> code line 2 >> code line 3 >> code line 4 >> code line 5 >> raise Class.new >> code line 7 >> code line 8 >> code line 9 >> >> rescue Class >> #here in some cases send back execution to code line 7 >> >> ensure >> end > > 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. 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. > 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> > > Kind regards > > robert