[plt-scheme] Displaying an error message and continuing

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Wed Jan 17 19:10:38 EST 2007

At Wed, 17 Jan 2007 15:58:07 -0800 (PST), Greg Woodhouse wrote:
> I have a situation where I want to be able to break out of a loop (actually, a 
> named let) and continue with the next iteration upon catching exn:fail:user 
> (or perhaps a subclass). The idea is that most errors should be fatal, but I'm 
> building a little REPL based application, and if input is badly formed, or if 
> certain run-time exceptions occur, I just want to go back to the input prompt. 
> Of course, I want breaks, and any errors that occur during evaluation other 
> than errors in my interpeter to terminate the loop.
> 
> Now, I'm using with-handlers, and simply use (loop), where loop is the label 
> in the named let, to "continue" with the next iteration. Now, here's the 
> problem: Instead of using (error ...) to raise an exception, I'm using (raise-
> user-error ... ) so that I get an instance of exn:fail:user, but I want to be 
> able to display the error string that is used in the call to raise-user-error. 
> Is this possible? Do I need an error display handler (not that I would know 
> how)?

I think you want to call the current error display handler.

Here's a little REPL that stops on most exceptions, but prints the
error message and continues if you evaluate an expression like
`(raise-user-error "keep going")':

  (let loop ()
    (with-handlers ([exn:fail:user?
                     (lambda (exn)
                       ((error-display-handler)
                        (exn-message exn)
                        exn))])
      (display "\n>> ") (flush-output)
      (print (eval (read))))
    (loop))


Matthew



Posted on the users mailing list.