Trapping file ops errors

Started by dukester, September 16, 2009, 01:44:15 PM

Previous topic - Next topic

dukester

Hey all....



Fooling around, getting comfortable with newLISP! Currently learning to load modules; trap load errors; checking for existence of files, etc.  I cobbled together the following:


(define (load_mod x)
(if (file? (append (env "NEWLISPDIR") "/modules/" x)) (print "Loading module: " x)
(throw-error  "module does not exist")
)
)


So, obviously, I'm first checking whether or not the required file "x", exists. If it does, it will get loaded (I have only a debug msg. at the moment).



Is there a way to absolutely tell that the file did get loaded?



Should "load_mod" be called from within a "catch", e.g.



(catch (load_mod "cgi.lsp")) ?
duke

cormullion

#1
You might be able to get away with something as simple as this:


(define (load-module x)
    (set 'module (append (env "NEWLISPDIR") "/modules/" x))
    (catch (load module) 'error))

(load-module "cgi.lsp")


which returns true or nil. If nil, you could do something with the error message stored in 'error.


(unless (load-module "cg.lsp")
   (println "sorry dude: " error (exit)))

dukester

#2
Cool!



I have to admit that this "catch-throw" thing is throwing me for a loop. I'll just have to study some more code.



Thanks for the input.
duke