0

I made a program that takes values from treeview and calculate something when button is pressed. I put try / except statement inside that function.

def SumAll():
    try:
       #do something (calculate)

    except ValueError:

        Error=messagebox.showinfo("Enter proper values")
        pass

The problem is, program keeps running when messagebox.showinfo appears, and it gives the ValueError. How can I fix that, and how can I put more than one error exception (IndexError, etc)?

2
  • 1
    Your question is somewhat unclear - what are you expecting the program to do? Where does the user enter the values? Inside SumAll()? Without a Minimal, Complete, and Verifiable example, it's hard to give advice. Commented Apr 21, 2018 at 8:00
  • A user enters values directly in treeview (its 80 lines of code, so thats why i didn't post that part). When user presses the button, the program should divide values from two columns. But for example if user does not enter the value in one column or enters letters instead of numbers , the program should show (messagebox.showinfo) and not show the error in coamnder, but the program does both. Commented Apr 21, 2018 at 8:09

2 Answers 2

1

You can re-raise the exception, and if the exception reaches the top of the stack, the program will exit

try:
    #do something (calculate)
except ValueError:
    Error=messagebox.showinfo("Enter proper values")
    raise

Or you can manually call sys.exit

import sys

try:
    #do something (calculate)
except ValueError:
    Error=messagebox.showinfo("Enter proper values")
    sys.exit(1)

To catch more than on in the same handler, you can do something like

try:
    #do something (calculate)
except (IndexError, ValueError):
    Error=messagebox.showinfo("Enter proper values")
    raise

or if you want different handlers you can have

try:
    #do something (calculate)
except IndexError:
    Error=messagebox.showinfo("Some message")
    raise
except ValueError:
    Error=messagebox.showinfo("Enter proper values")
    raise
Sign up to request clarification or add additional context in comments.

2 Comments

It does not work, it keeps showing error in commander. A user enters values directly in treeview (its 80 lines of code, so thats why i didn't post that part). When user presses the button, the program should divide values from two columns. But for example if user does not enter the value in one column or enters letters instead of numbers , the program should show messagebox.showinfo and not show the red error in commander, but the program does both.
@aleksandarbeat Can you edit the question with enough information to reproduce your setup? minimal reproducible example
0

You can catch multiples by:

try:
    k = input().split()[2]
    i = int(k[0])
except (IndexError, ValueError) as e:
    print(e)  # list index error: 'Hello World', Value error: 'Hello World Tata'
else:
    print("no error")

this will guard against inputs that split to less then 3 items '5 a' as well as against int conversion errors: 'Hello World Is A Cool Thing' (`'Is'`` is not an integer).

Use How to debug small programs (#1) to debug through your real program, the error you get above this except is caught. You probably get a resulting error somewhere else because somethings not right with the return of your function.

1 Comment

It does not work, it keeps showing error in commander. A user enters values directly in treeview (its 80 lines of code, so thats why i didn't post that part). When user presses the button, the program should divide values from two columns. But for example if user does not enter the value in one column or enters letters instead of numbers , the program should show messagebox.showinfo and not show the red error in commander, but the program does both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.