Page 1 of 1

Trapping file ops errors

Posted: Wed Sep 16, 2009 8:44 pm
by 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:

Code: Select all

(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")) ?

Posted: Thu Sep 17, 2009 4:58 pm
by cormullion
You might be able to get away with something as simple as this:

Code: Select all

(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.

Code: Select all

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

Posted: Fri Sep 18, 2009 3:26 am
by dukester
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.