Python Error Checking

In a previous section, you set up a function that checked for errors. If the user didn't input a number, we had this:

if hero.isdigit() == False:

print("Not a number")

exit()

This checked a variable to see if it was a digit or not. If it wasn't we printed something out. We then exited the program. There is another way to check for errors, though. With a try … except block.

The idea is to try some code. If something codes wrong, you catch it in an except part. The except keyword is short for exception. Let's see how it works.

Start a new project for this. In a new Python file, enter the following:

number = input("Enter a number: ")

Now print it out with this:

print("Your number times 10 is:", int(number) )

So the user enters a number, which is then placed in the number variable. We try to convert that number to an integer with int(number) inside of the print statement.

Run your code. In the output window, type the text 'one' instead of the number one:

Python output window asking for input

Press the enter key on your keyboard and you should see this error message:

A value error in Python

If you get an error like that it's called an exception. This one tells you which line is causing the problem. It also says this:

ValueError: invalid literal for int() with base 10: 'one'

This type of error this is called a ValueError. You normally see it when Python has a problem with the numbers you enter. Our ValueError was because we tried to convert the string 'one' to an integer. The string was coming from the input box. We didn't check to see if it was a digit or not, like we did before. Other types are ZeroDivisionError, when you try to divide a number by zero, and NameError when Python can't recognise a variable name you used in your code.

You can check for exceptions like the one above with a try … except block. The format is this:

try:

# YOUR CODE HERE

except exception_type:

# YOUR ERROR MESSAGE HERE

So you need the word try followed by a colon. The code you want to try goes next, on indented lines. Next, you need the word except followed by an exception type. After a colon, add your error message.

Change your code to this:

number = input("Enter a number: ")

try:

print("Your number times 10 is: ", int(number))

except:

print("Was that a number?")

Don't run your code yet. Have a look at what PyCharm has underlined in your code. It has underlined the word except. Hold your mouse over except and you'll see a message:

Too broad exception clause. Do not use bare except

You won't get an error if you run your code, however. Enter the text 'one' instead of the number 1 and you'll see 'Was that a number?' printed in the output window.

The reason PyCharm is flagging up a problem is because it's highly recommended that you have one of the exception types after the keyword except. For our code, the type was ValueError. Change the exception line to this:

except ValueError:

print("Was that a number?")

The underline should go away in PyCharm. You still get the same error message when you run the code, though.

The point of using the try … except block, however, is so that your programs don't crash. If they are crashing, make a note of the type error, such as the ValueError above. You can then add that type error after the except part.

To see a list of type errors, see this page:

https://docs.python.org/3/library/exceptions.html#bltin-exceptions

There are quite a lot of them!

It's always a good idea to try and write code that doesn't throw up errors. And, if you are getting errors, to solve them in your code. This is not always possible. For example, in a later section, you'll see how to open up a text file. But you can never know if somebody has moved the file you're trying to open. In this case, you'd need a try … except block to deal with the problem.

There's a lot more to try … except blocks. If you want to explore the topic more fully, see here:

https://docs.python.org/3/tutorial/errors.html


Let's move on. We'll do some more things with tkinter and graphic user interfaces.

Basic GUI Design >