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

**URL:** <https://forum.inductiveautomation.com/t/solved-try-except-syntaxerror-not-executing-inside-project-scripts-correclty/33628>\
**Category:** Ignition\
**Created:** [April 2, 2020, 3:04pm UTC](https://forum.inductiveautomation.com/t/solved-try-except-syntaxerror-not-executing-inside-project-scripts-correclty/33628 "2020-04-02T15:04:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [April 2, 2020, 3:04pm UTC](https://forum.inductiveautomation.com/t/solved-try-except-syntaxerror-not-executing-inside-project-scripts-correclty/33628/1 "2020-04-02T15:04:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 2, 2020, 3:05pm UTC](https://forum.inductiveautomation.com/t/solved-try-except-syntaxerror-not-executing-inside-project-scripts-correclty/33628/2 "2020-04-02T15:05:58Z")

</div>

`SyntaxError` - one word; it’s a constant.

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [April 2, 2020, 3:11pm UTC](https://forum.inductiveautomation.com/t/solved-try-except-syntaxerror-not-executing-inside-project-scripts-correclty/33628/3 "2020-04-02T15:11:28Z")

</div>

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'))
```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 2, 2020, 3:16pm UTC](https://forum.inductiveautomation.com/t/solved-try-except-syntaxerror-not-executing-inside-project-scripts-correclty/33628/4 "2020-04-02T15:16:45Z")

</div>

> [@bmeyers](#):
>
> 21, '\t\t\tif stayInLoop == 1\n'))

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

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [April 2, 2020, 3:39pm UTC](https://forum.inductiveautomation.com/t/solved-try-except-syntaxerror-not-executing-inside-project-scripts-correclty/33628/5 "2020-04-02T15:39:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 2, 2020, 3:49pm UTC](https://forum.inductiveautomation.com/t/solved-try-except-syntaxerror-not-executing-inside-project-scripts-correclty/33628/6 "2020-04-02T15:49:46Z")

</div>

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`:

```auto
try:
   NewScript.lookupStuff()
exceptSyntaxError:
   pass

```

---

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [April 2, 2020, 3:51pm UTC](https://forum.inductiveautomation.com/t/solved-try-except-syntaxerror-not-executing-inside-project-scripts-correclty/33628/7 "2020-04-02T15:51:45Z")

</div>

Yep, that did the trick.

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