exceptions cmsc 201. overview exceptions are run-time errors, especially ones that the programmer...
TRANSCRIPT
![Page 1: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/1.jpg)
Exceptions
CMSC 201
![Page 2: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/2.jpg)
Overview
Exceptions are run-time errors, especially ones
that the programmer cannot predict.
example 1: division by zero
example 2: user enters "garbage" data
example 3: disk full
![Page 3: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/3.jpg)
Vocabulary
When some piece of code causes a run-time error,
we say that the code throws an exception or that it
raises an exception.
The part of a program that deals with the run-time
error catches the exception or handles the
exception.
![Page 4: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/4.jpg)
Divide by Zero
totalBill = 67
n = int(input("Number of people? "))
share = totalBill / n
print("Share of the bill is ", share)
If user enters 0, Python complains and terminates:
Traceback (most recent call last):
File "divide_by_zero.py", line 3, in <module>
share = totalBill / n
ZeroDivisionError: division by zero
![Page 5: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/5.jpg)
Exception Handling
try:
totalBill = 67
n = int(input("Number of people? "))
share = totalBill / n
except ZeroDivisionError :
print("Customers ran away")
else:
print("Share of the bill is ", share)
![Page 6: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/6.jpg)
Syntax for Exceptions
try:
block of code that might causeone or more types of exceptions
except ExceptionType1 :block of code to handle ExceptionType1
except ExceptionType2 :block of code to handle ExceptionType2
...else:
block of code to execute when no exceptions found
![Page 7: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/7.jpg)
Exception Types
How to find exception types?
1. Read the friendly manual (RTFM)
2. Google (really, same as RTFM)
3. Make Python tell you:
Traceback (most recent call last):
File "divide_by_zero.py", line 3, in <module>
share = totalBill / n
ZeroDivisionError: division by zero
![Page 8: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/8.jpg)
Exception Types
>>> userInput = input("Enter a number: ")
Enter a number: abc
>>> n = int(userInput)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'abc'
>>>
![Page 9: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/9.jpg)
Exception Types
>>> userInput = input("Enter a number: ")
Enter a number: ^D
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError
![Page 10: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/10.jpg)
Badgering the user for input
done = False
while not done: try: userInput = input("Enter a number: ") n = int(userInput) except ValueError: print("That's not an integer! Try again.") else: print("Thank you!") done = True
print("n is ", n)
![Page 11: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/11.jpg)
Badgering the user for input
done = False
while not done: try: userInput = input("Enter a number: ") n = int(userInput) except ValueError: print("That's not an integer! Try again.") else: print("Thank you!") done = True
print("n is ", n)
![Page 12: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/12.jpg)
Badgering the user for input
done = False
while not done: try: userInput = input("Enter a number: ") n = int(userInput) except ValueError: print("That's not an integer! Try again.") except EOFError: print("Please type something! Try again.") else: print("Thank you!") done = True
print("n is ", n)
![Page 13: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/13.jpg)
Raising an ExceptionYou can write code that raises exceptions:
try: raise ZeroDivisionError
except ZeroDivisionError: print("Did someone divide by zero?")
else: print("Everything is hunky-dory")
More useful later when we look at functions
![Page 14: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict. example 1: division by zero example](https://reader036.vdocuments.net/reader036/viewer/2022082821/5697bfd41a28abf838cacee4/html5/thumbnails/14.jpg)
BaseException• The BaseException type matches all
exceptions, even ones you don't know about.
• Use this very carefully! Might not be a good
idea.
• What can you do if you catch a BaseException?
o exit the program slightly more gracefully.
o return to home state (if this is possible).
o re-throw the exception (requires more syntax
and not clear what is accomplished).