newLISP Fan Club

Forum => newLISP in the real world => Topic started by: dukester on September 16, 2009, 01:44:15 PM

Title: Trapping file ops errors
Post by: dukester on September 16, 2009, 01:44:15 PM
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")) ?
Title:
Post by: cormullion on September 17, 2009, 09:58:21 AM
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)))
Title:
Post by: dukester on September 17, 2009, 08:26:23 PM
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.