[SOLVED] Try Except SyntaxError Not executing inside project scripts correclty

Good morning.

I have a function here:

def lookupStuff():
    try:
       system.gui.me//////ssageBox('doing some stuff')
    except Syntax Error:
        system.gui.messageBox('syntax error found')

A button on my GUI causes this function to run. When I run the above code nothing actually happens on the GUI. If I remove the syntax errors though i get the proper message “doing some stuff”.

With the GUI open I do ctrl+ shift + F7 and under the console I can see that SyntaxError did actually occur.

How can I get my try except SyntaxError to function properly?

SyntaxError - one word; it’s a constant.

Ok, so I fixed that. Thank you, but now I get the following error on my screen.

Traceback (most recent call last):
  File "<event:mouseClicked>", line 1, in <module>
SyntaxError: ("mismatched input '\\n' expecting COLON", ('<module:project.lookupStuff>', 26, 
21, '\t\t\tif stayInLoop == 1\n'))

You're opening a block (if stayInLoop == 1) but not terminating it with a colon.

1 Like

Let me start over one more time. Under project I create a new script called ‘NewScript’. I type in the following code:

def lookupStuff():
    try:	
	    x=2
	    if x == 2
		    system.gui.messageBox('doing some stuff')
    except SyntaxError:
	    system.gui.messageBox('syntax error found')

On line 4 I can now see on the faaaaar right a red box appear telling me I have a SyntaxError. Instead of fixing the error I save the code and click my button which calls project.NewScript.lookupStuff().

Instead of a message ‘syntax error found’ I get an Error window with the following.

Traceback (most recent call last):

  File "<event:mouseClicked>", line 1, in <module>

SyntaxError: ("mismatched input '\\n' expecting COLON", ('<module:project.NewScript>', 4, 11, '\t\tif 
x == 2\n'))

Ignition v7.9.10 (b2018112821)
Java: Oracle Corporation 1.8.0_241

So what I think is happening if Ignition is saying I can’t run any code with SyntaxError. I will never get to except SyntaxError because Ignition won’t allow it to get that far.

Is that a true statement?

Not exactly. Python has to ‘run’ a module through once to ‘compile’ it; that’s where the SyntaxError is being thrown, not when actually calling your code. If you change your code at the call site to catch the SyntaxError, it should work; eg, in mouseClicked:

try:
   NewScript.lookupStuff()
exceptSyntaxError:
   pass
1 Like

Yep, that did the trick.

You also cleared up a lot of confusion about error catching so thank you for the help.

1 Like