This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Trapping errors




SG> Hi.  I'm sorry if this is an RTFM but I've checked all of the FMs
SG> I can find and it's not covered.

SG> My application gets a file name and then tries to open the file.
SG> If the file dosen't exist then the application stops.  I'd like to
SG> be able to catch the error and handle it a bit more gracefully.
SG> Can someone point me to a way to accomplish this?

In the available reference manual (in Info), you can read about CATCH
and THROW. Whenever there is an error, Guile throws an exception that
can be caught by CATCH. Here is an example of a handler that catches
any error, but the file open error in particular. 

  (use-modules (ice-9 slib))
  (require 'format)

  (catch #t 
         (lambda () 
            (with-input-from-file "greenberg" 
                                  (lambda ()
                                     (display (read)))))
         (lambda (key args . optionals) 
            (display (format "Key:\t~a\nArgs:\t~a\nOptionals:\t~a\n" 
                             key args optionals))))

Here is the output from this code:
  Key:	system-error
  Args:	open-file
  Optionals:	("%s: %S" ("No such file or directory" "greenberg") (2))


So your error handler should catch a symbol 'system-error and test if
its argument is 'open-file.

	Lars

-- 
Lars Arvestad               Dept. of Numerical Analysis and Computing Science
                       Royal Institute of Technology (KTH), Stockholm, Sweden

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]