PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 30, 2022

[FIXED] How do I detect the EOF parser error from within the code?

 October 30, 2022     eof, python-3.x, tkinter     No comments   

Issue

The Code is for a GUI Calculator. How do I detect the EOF parsing error from within my code?

Code:

def btnEqualsInput():
    global operator
    if operator!='':
        sumup = str(eval(operator))
        text_Input.set(sumup)
        operator =""

Output when I click '=' with 3* in textbox

    sumup = str(eval(operator))
  File "<string>", line 1
    3*
     ^
SyntaxError: unexpected EOF while parsing

I want to display "Error!" in the calculator display whenever user presses Equals on wrong syntax in the textbox.


Solution

Just catch the exception:

def btnEqualsInput():
    global operator
    if operator!='':
        try:
            sumup = str(eval(operator))
            text_Input.set(sumup)
            operator =""
        except SyntaxError as e:
            print("Error!",str(e)) #e contains the type of message, for example  unexpected EOF while parsing

You can parse the error string as well if you want to do something specific (for EOF for example, "EOF" in str(e) will be true)



Answered By - kabanus
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing