Exception not being handled by the try-except block in Python

Prateek Jaiswal

I have written a simple python code, to Input any number from the user. In case anything else is entered an exception is raised as per the code. I typecast the entered value in into int just to check whether it is an integer. Ideally i am excepting that when i enter any alphabet, the exception should be raised and should print the text that i have given. But still i can see that its not being caught.

However, when i specifically add one more try-except block around the typecast statement, it works.


from tkinter import *

window = Tk()
window.geometry('400x400')

class hangman:
    def __init__(self):
        self.entry = ''

    def clicked(self, label2):
        label2.place(x=100, y=200)

        while True:
            try:
                def get_value(event):
                    self.entry = e1.get()
                    self.entry = int(self.entry)
                    print(self.entry)

                Label(window, text="Enter any number :").place(x=10, y=220)

                e1 = Entry(window)
                e1.place(x=10, y=240)
                e1.bind('<Return>', get_value)  #To get the value entered in the entry when Return is pressed.
                print("Past bind1")
                print(self.entry)
                print("Past bind2")
                break

            except ValueError as e :
                print("\n\tPlease Enter only Numbers!!")


obj1    = hangman()
label2  = Label(window, text="Start")
bt      = Button(window, text="Play", command=lambda: obj1.clicked(label2))

bt.place(x=150, y=125)

window.mainloop()

I expect the exception to be caught and the print my message instead of the standard exception error.

Stanisław Wilczyński

If you put try/except block in get_value function the exception is caught properly:

from tkinter import *
window = Tk()
window.geometry('400x400')


class hangman:
    def __init__(self):
        self.entry = ''

    def clicked(self, label2):
        label2.place(x=100, y=200)

        while True:
            def get_value(event):
                try:
                    self.entry = e1.get()
                    self.entry = int(self.entry)
                    print(self.entry)
                except ValueError as e:
                    print("\n\tPlease Enter only Numbers!!")

            Label(window, text="Enter any number :").place(x=10, y=220)

            e1 = Entry(window)
            e1.place(x=10, y=240)
            e1.bind('<Return>', get_value)  # To get the value entered in the entry when Return is pressed.
            print("Past bind1")
            print(self.entry)
            print("Past bind2")
            break


obj1 = hangman()
label2 = Label(window, text="Start")
bt = Button(window, text="Play", command=lambda: obj1.clicked(label2))

bt.place(x=150, y=125)

window.mainloop()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is the error not being caught/handled in this "try...except" block?

Python: Propagate an exception through a Try/Except Block with multiple Excepts

How to remove duplicate exception error in python try/except block?

Handle multiple exception in Python with only one try - except block

Exception isn't handled while occuring in a try block in C++

Python: Except block capturing exception that is not listed for block

Using python "with" statement with try-except block

Not wanting to use if else but try except block in python

python Shorten the embedded try-except block

Python code crashes within try except block

Python Unit Test for Try/Except Block

Avoid multiple try except block in python

Try except block python varies result

Django exception handler for the default-brench in try-except block

How to test the exception of try/except block using pytest

try-except block: analogue for 'else' if exception was raised

Exception is thrown even though there is a try...except block

Using a Try and Except block, but in the Except Part it says Not Defined Python

Python nested try/except - raise first exception?

Exception Handling in Python (Try...Except)

Scope of a try/except block

python catch exception and continue try block

try except not catching the exception

Exception Handling: exception being handled multiple times

Looking for an alternative for nested try-except block in python

File has zero lines in else block of python try except code

Is Python *with* statement exactly equivalent to a try - (except) - finally block?

Python: How to simplify multiple statements with the same try/except block

Python Throwing Error After Passing Try - Except Block