Catching errors

Q&A's, tips, howto's
Locked
pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Catching errors

Post by pjot »

Hi Lutz,

In my current project I would like to catch errors myself. Newlisp seems to have a facility for this: error-event.

But I seem not able to get it working:

Code: Select all

#!/usr/bin/newlisp

(context 'DEMO)

(define (echo)
	(println "Hello world")
)

#-----------------------------------

(context 'MAIN)

(define (my-handler)    
	(println "Error # " (error-number) " has occurred.")
)

(error-event 'my-handler)

(DEMO:eecho)

(exit)
When I run this program, nothing happens! I never see my own error message. It should produce code 23. What is wrong? I am using newLisp 8.4.9 on Linux.

Peter

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Pasting in one after another in WIN 8.4.10

Code: Select all

newLISP v.8.4.10 on Win32 MinGW.

> (context 'DEMO)
DEMO
DEMO> (define (echo) (println "Hello world"))
(lambda () (println "Hello world"))
DEMO> (context 'MAIN)
MAIN
> (define (my-handler)(println "Error # " (error-number) " has occurred."))
(lambda () (println "Error # " (error-number) " has occurred."))
> (error-event 'my-handler)
my-handler
> (DEMO:echo)
Hello world
"Hello world"
> (DEMO:eecho)
Error # 23 has occurred.

>  
Hans-Peter

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Observation on this:

When I paste the code completly into the tk-editor window and press 'Evaluate Editbox' the left top context list window is not updatet with the new context. Closing and reopening shows it up.
Hans-Peter

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Hm, so it works in interactive mode?

Does the program work in Windows when run like:

C:\>newlisp demo.lsp?

----
Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

In the editor/browser hit the File/Refresh button or do a Ctrl-R, that will refresh the context list.

To go into the context click on the context, now everything you type in the editor window will be translated into the context selected.

Lutz

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

...but my demo program still doesn't work in Linux... am I doing something wrong here?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Error handlers using 'error-event' work only when the program is loaded interactively, i.e. doing a (load "demo.lsp") from the interactive commandline or when using in a newlisp.so or newlisp.dll. (I will document this).

As a workaround write your error handling with "catch" and "throw"

Code: Select all

#!/usr/bin/newlisp

(context 'DEMO)

(define (echo)
   (println "Hello world")
)
(context 'MAIN)
#-----------------------------------

(if (not (catch (DEMO:eecho) 'result)) 
   (println result))
Lutz

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Thanks.

But that is too bad: if somebody is creating a context DEMO for general use, and the newLisp programmer wants to use this context DEMO, it seems there is no way for that context to catch erronuous invocations? I mean, it's up to the invoker of the context to catch his own errors?

So what happens if you want to deinitiliaze something in that context DEMO (free resources, kill processes, delete temporary files)? The user doesn't know since the DEMO context has performed some actions behind his back.

I am looking for a way to let the DEMO context catch any errors, and before exiting newLisp, let this context cleanup it's own mess.

Is this possible?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

You can do error catching inside a context. Using 'catch'.

To use a function in DEMO you would always have to prefix functions with DEMO, I mean its just the same as with any other module or object system: you write the module and publish and interface, where you describe the demo:func, dem:foo etc and how you use them. I mean when you call some class function in Java, you also don't know whats really going on there until you look into the code.

Lutz

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Thats right. But I mean something else...

Suppose a context creates temporary files, and the user of this context makes a mistake, by calling a function in a wrong way - then newLisp exits and these temporary files would remain on disk.

Also if a context starts a process, and newLisp exits because of some error of the user - then the created process remains in memory.

Since the user of the context does not know which files were created or which processes were started, he can never clean this up.

Therefore, if a context creates files or processes, it seems that this context also is responsible for cleaning up. A sudden exit of newLisp (because of an error by the programmer using the context) prevents the context from doing so.

Error catching inside a context only helps the creator of the context. But the user of the context, calling functions while being in the MAIN context for example, can make a mistake: newLisp exits, and the temporary files and processes remain.

Again, I am just wondering if it is possible at all for newLisp to capture a sudden exit (in case an error is encountered), in a way that a context can cleanup it's own mess. If not, also fine, but then the user of the context should be very careful with invoking the context functions.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Ok, I think I understand, you want somer errorhandling like "catch anything going bad in this context". No unfortunately it does not exist. You only can catch errors function specific.

So the only work around would be to catch your sufficiently high up in the function calling hierarchy. In the end each program starts with one function and you catch the error conditions of that function and you catch everything underneath. But there is no possibility to catch things context specific.

Lutz

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

OK thanks Lutz.

Locked