This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Trapping errors
Hello all--
The short (unhelpful) answer is to use the file-exists? primitive:
(file-exists? "/etc/passwd")
The other relevant primitive you'd probably find interesting is named access?:
(access? "/etc/passwd" R_OK)
(access? "/etc/passwd" W_OK)
(access? "/etc/passwd" X_OK)
The "real" answer to your question involves the catch/throw primitives.
I've included below the relevant section from the guile-reference manual:
- primitive: catch KEY THUNK HANDLER
Invoke THUNK in the dynamic context of HANDLER for exceptions
matching KEY. If thunk throws to the symbol KEY, then HANDLER is
invoked this way:
(handler key args ...)
KEY is a symbol or #t.
THUNK takes no arguments. If THUNK returns normally, that is the
return value of `catch'.
Handler is invoked outside the scope of its own `catch'. If
HANDLER again throws to the same key, a new handler from further
up the call chain is invoked.
If the key is `#t', then a throw to *any* symbol will match this
call to `catch'.
- primitive: throw KEY &rest ARGS ...
Invoke the catch form matching KEY, passing ARGS to the HANDLER.
KEY is a symbol. It will match catches of the same symbol or of
#t.
If there is no handler at all, an error is signaled.
- procedure: error MSG ARGS ...
Raise an error with key `misc-error' and a message constructed by
displaying MSG and writing ARGS.
Hope this helps.
--Brad
Steven Greenberg writes:
> Hi. I'm sorry if this is an RTFM but I've checked all of the FMs I can find and it's not covered.
>
> Anyway...
>
> My application gets a file name and then tries to open the file. If the file dosen't exist then the application stops. I'd like to be able to catch the error and handle it a bit more gracefully. Can someone point me to a way to accomplish this?
>
> Many thanks,
> Steve
>
>
> begin:vcard
> n:Greenberg;Steven
> tel;fax:650.210.0710
> tel;work:650.210.2335
> x-mozilla-html:TRUE
> url:http://www.epinions.com/
> org:Epinions
> adr:;;2037 Landings Drive;Mountain View;CA;94043;
> version:2.1
> email;internet:greenbes@epinions.com
> title:Engineer, iconoclast
> fn:Steven Greenberg
> end:vcard