Trapping file ops errors

Q&A's, tips, howto's
Locked
dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Trapping file ops errors

Post 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")) ?
duke

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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)))

dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Post 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.
duke

Locked