4

I was just wondering if a language could support something like a Retry/Fix block?

The answer to this question is probably the reason it's a bad idea or equivalent to something else, but the idea keeps popping into my head.

void F()
{
  try
  {
    G();
  }
  fix(WrongNumber wn, out int x)
  { 
    x = 1;
  }
}

void G()
{
  int x = 0;
  retry<int>
  {
    if(x != 1) throw new WrongNumber(x);
  }
}

After the fix block ran, the retry block would run again...

9
  • 3
    How would that be different from a catch block?
    – Caleb
    May 30 '12 at 22:22
  • because after the fix block the retry block would run again? that's what i was thinking at least. May 30 '12 at 22:23
  • 3
    We already have it, it's named Eiffel. I do remember a similar clause in a yet another language which name I cannot recollect now.
    – 9000
    May 30 '12 at 22:23
  • 6
    Yet another obscure control structure a lisp hacker can implement with call/cc and a thirty line macro... that is, if they didn't already do it.
    – user7043
    May 30 '12 at 22:25
  • 1
    Even Visual Basic has it: On Error Goto X / Resume Next
    – MSalters
    May 31 '12 at 9:06
3

Yes, a language could do that.

There are examples in existing languages. Common Lisp provides a system which allows an exception (condition, in CL terminology) handler to return control to the point at which the exception was thrown, passing extra information about how the condition should be handled.

A good description of this is available in the book Practical Common Lisp, 19. Beyond Exception Handling: Conditions and Restarts

As other commenters have mentioned, Scheme's general continuation system could be used to implement this, and Eiffel provides similar functionality.

Thanks to @9000 and @delnan for bringing up Eiffel and call/cc in the comments on the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.