Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!spool.mu.edu!uunet!mcsun!unido!opal!net From: net@opal.cs.tu-berlin.de (Oliver Laumann) Newsgroups: comp.lang.scheme Subject: Re: open-input-file [topics from hell, part 2] Message-ID: <3158@kraftbus.cs.tu-berlin.de> Date: 25 Apr 91 09:17:39 GMT Article-I.D.: kraftbus.3158 References: <9104211907.AA25632@august> <5387@goanna.cs.rmit.oz.au> Organization: Technical University of Berlin, Germany Lines: 35 ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes: > The only Scheme implementation I actually understand is Elk, and it > would be very easy to add this to Elk. In Elk one can easily write a function that calls open-input-file with a file name and, if the file cannot be opened, invokes a user-supplied error handler like this: (define (open-input-file-with-error-handler file handler) (call/cc (lambda (return) (fluid-let ((error-handler (lambda args (apply handler return args)))) (open-input-file file))))) In this example, the user-supplied error handler is invoked with a continuation, which, when called, returns a value to the caller of open-input-file-with-error-handler and an error message (a symbol identifying the function that failed, a format string, and optional arguments). The function could be used like this: (define (my-handler return . error-message) ;; do something (e.g. print error message) (return #f)) (let ((port (open-input-file-with-error-handler '/etc/passwd my-handler))) (case port (#f ;; file could not be opened (else ;; do something with the port In case the user-supplied error handler terminates normally, Elk would invoke the standard error handler supplied by the top level (which prints the error message in a standard way and resets to the top level).