Download presentation
Presentation is loading. Please wait.
1
Exception Handling
2
Programming Challenge
Write a program that simulates the guessing magic number game with numbers one through ten BUT before you start the game, ask the user whether they want to play Loop the game until the user guesses the correct number Then, ask the user if they would like to play again? If the user types in “no” print “Okay, goodbye”
3
Programming Challenge
Continue from the previous challenge and now write your program so that if the user types in anything other than “yes” or “no”, the program says “Sorry, I didn’t get that” and then loop from the beginning This is what we call exception handling!
4
Programming Challenge
Now, try to make your program crash! How did you make it crash? Why did it crash?
5
Programming Challenge
This is what we call a runtime error. Some of you have been asking about these, “What if the user types in a letter when you wanted an integer?” Try to adjust your code so that it will NEVER crash
6
Exceptions An exception is an error that causes a program to halt while it’s running In other words, it something that causes a runtime error Example: X = “Donald” Y = int (X)
7
Exceptions When an exception occurs Python will generate a “traceback”
Tracebacks give information about the exception that occurred, including the line number that caused the issue We normally say an exception has been “raised” by the program >> int(“The Donz”) Traceback (most recent call last): File “<pyshell#0>”, line 1, in <module> int(“The Donz”) Value Error: invalid literal for int() with base 10: “Donald”
8
Preventing Exceptions
Many times we can avoid exceptions all together by adjusting how the code is written. For example, in the following code an exception will be raised if x = 0: x = int( input( “give me a number”) ) print (100 / x)
9
Preventing Exceptions
We can avoid the potential exception by including the second line of code in a selection statement x = int( input( “give me a number”) ) if x != 0: print (100 / x)
10
Catching Exceptions Python has an exception handling statement that can be used to “catch” exceptions and prevent them from crashing your program We use the word “try” to tell Python to test for a valid code or input from the user It comes along with the words “except” which runs if an exception is raised and “else” which executes only if the input of the “try” block was satisfactory
11
Catching Exceptions try: # questionable code except: # this line of code runs if the code in the “try” block # raised an exception else: # this block runs if the code in the “try” block was run # successfully without exceptions
12
Data Validation You can use the Try / Except statement to validate the data type of inputted data try: x = int(input(“give me a number”)) except: print (“that’s not an integer”) else: print(“great!”)
13
Data Validation As we saw in the “if” selection statements, the “else” statement is not necessary in our try / exception statements try: x = int(input(“give me a number”)) print(x) except: print(“that’s not a number”)
14
Data Validation You’ll notice that the “except” block is executed the moment an exception is raised The rest of the “try” block will not execute if an exception was previously raised
15
Data Validation We can also loop a try / except statement so that the user will be continually prompted until they enter a valid input
16
Programming Challenge
Write a program that continually prompts the user for the price of an item until they enter a valid price
17
Programming Challenge
while True: try: x = int(input(“price: ”)) break except: print(“not a valid input”)
18
Finally The try / except statement can include one additional block called the “finally” This block will always execute at the end of the try / except statement (even after the word “break” in a loop)
19
Finally try: x = int(input(“give me a number”)) except: print (“that’s not an integer”) else: print(“great!”) finally: print(“this will always print”)
20
Finally while True: try: x = int(input(“give me a number”)) break
except: print (“that’s not an integer”) finally: print(“this will always print”)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.