Notifications
Clear all

[Closed] Difficulty with error handling

 (
global rltTest
try destroyDialog rltTest catch()
 
rollout rltTest ""
(
	pickbutton pbObject "Pick" 
	button btnDoIt "Do it"
 
	local theObject
 
	on pbObject picked obj do
	(
		theObject = obj
	)
 
	on btnDoIt pressed do
	(
		theObject.name = "myObject"
		theObject.pos = [0,0,0]
	)
)
createDialog rltTest 60 60
)

In the code above, someone pointed out that if the user picks the object, then deletes it and presses the Do it button, then the script crashes.
I’ve searched the reference to find a way for the script totrelaunch itself and the closest I could find was this:

 try theObject.name = "myObject" catch(fileIn (getSourceFileName())) 

Still, no joy. The script does restart, thus replacing the dialog by a new one. But at the same time it crashes at the next line. Beep + error in the listener.

How can I deal with this error?

5 Replies

Hello Caprier,

MXS has a general event callback system which is called when a node is deleted. search for callbacks in the help.
Also, there is a the “when construct” although I would recommend the first option since it is much less intensive


fn whatnode=
(
 n=callbacks.notificationparam()
 format "node deleted:%
" n
)

callbacks.addscript #nodepredelete "whatnode()" id:#nodetest
/*
callbacks.removescripts id:#nodetest
*/

Josh.

Hi Pat,
rather than restarting the script after crash, avoid it by wrapping your code in a node validity test. If it’s valid, execute the code, otherwise do nothing and reset the node storing variable.


(
    rollout rltTest ""
    (
        pickbutton pbObject "Pick" 
        button btnDoIt "Do it"
     
        local theObject
        on pbObject picked obj do
        (
            theObject = obj
        )
     
        on btnDoIt pressed do
        (
            -- Generic Node Validity Test Condition
            if (isValidNode theObject == true) then
            (
                theObject.name = "myObject"
                theObject.pos = [0,0,0]
            )
            else
            (
                theObject = undefined
            )
/*
            -- OR: Specific Node Deletion Test Condition
            if (isDeleted theObject == false) then
            (
                theObject.name = "myObject"
                theObject.pos = [0,0,0]
            )
            else
            (
                theObject = undefined
            )
*/
        )
    )
    try (destroyDialog rltTest) catch()
    createDialog rltTest 60 60
)

  • Enrico

*You could make the DO IT button grayed out and enable it only when a valid object has been picked with the PICK button.

*You can check if the object is valid (if isValidNode theObject then …) inside the DO IT handler so even the object is deleted between the pick and do it clicks it would not crash.

Guys, thank you for all the answers.

@SyncViews and Bobo: your suggestion makes perfect sense. I don’t know why but I was a little reluctant to put 100 lines of code into a single test block. Is it common practice?

@j-man: I like the idea of a callback. So it could disable the DoIt button if the picked object is deleted, forcing the user to pick another one. Now I just have to figure out the syntax…

Great! I’ve finally got my mind around the callback mechanism and now it works like a charm.
Thanks a lot, people! Have a bear on me!! :bounce: