Is there a way to stop script


#1

Is there a way to stop script if something went wrong
for example:

myObj = $
if ($ == undefined) then EXIT

rest of code

i tried, but get
Exit: not inside a loop


#2
myObj = selection[1]
if isValidNode myObj then
(
-- code here
)
else
(
messagebox "Error"
)

#3

I gave a bad example
i need the script to stop on every first error
and didn’t execute the rest of the code

new example^

if selection.count!=0 then (bla-bla-bla)
else (messagebox "Error"  and exit without continuing execution the script)

if isKindOf i helper!=undefined then (delete Helpers)
else (messagebox "Error"  and exit without continuing execution the script)

backgroundColor=color 255 255 255;
environmentMap=undefined;
lot of code
lot of code
lot of code
lot of code
lot of code
lot of code
rollout blablabla
lot of code
lot of code

#4
(
	if selection.count != 0 then
	(
		if isKindOf selection[1] helper != undefined then 
		(
			delete Helpers
			
			backgroundColor=color 255 255 255;
			environmentMap=undefined;
			--lot of code
			--lot of code
			--lot of code
			--lot of code
			--lot of code
			--lot of code
			rollout blablabla
			--lot of code
			--lot of code
		
		)
		else 
		(
			messagebox "Error"  --and exit without continuing execution the script
		)
		
	)
	else
	(
		messagebox "Error" -- and exit without continuing execution the script
	)


)

Or like this:

(
	continueScriptExecution = true
	if selection.count != 0 then
	(
		bla-bla-bla
	)
	else
	(
		continueScriptExecution = false
		messagebox "Error 1" title:""
	)
	
	if continueScriptExecution then
	(
		if isKindOf i helper!=undefined then 
		(
			delete Helpers
		)
		else 
		(
			continueScriptExecution = false
			messagebox "Error 2"  
		)
	)
	
	if continueScriptExecution then
	(
		backgroundColor=color 255 255 255;
		environmentMap=undefined;
		lot of code
		lot of code
		lot of code
		lot of code
		lot of code
		lot of code
		rollout blablabla
		lot of code
		lot of code
	)
)

#5

how do you use this code? if it’s a handler or a function you can use return()


#6
else
	(
		continueScriptExecution = false
		messagebox "Error 1" title:""
	)

it would be great, but it doesn’t work