Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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...

share|improve this question
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. –  Aaron Anodide 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
5  
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. –  delnan 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
show 4 more comments

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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