Introduction to Programming Quiz 8

5.0 (2 reviews)
Within the try clause of a try statement, you code ___.
- only the statements that might cause an exception
- all the statements of the program
- a block of the statements that are most likely to cause an exception
- a block of statements that might cause an exception
Click the card to flip 👆
1 / 8
Terms in this set (8)
Which of the following is the correct way to code a try statement that catches any type of exception that can occur in the try clause?
- try:
number = float(input("Enter a number: "))
print("Your number is: ", number)
- try:
number = float(input("Enter a number: "))
print("Your number is: ", number)
except:
print("Invalid number.")
- try:
number = float(input("Enter a number: "))
print("Your number is: ", number)
else:
print("Invalid number.")
- try:
number = float(input("Enter a number: "))
print("Your number is: ", number)
except ValueError:
print("Invalid number.")
Code Example 8-1
import csv
import sys
FILENAME = "names.csv"
def main():
try:
names = []
with open(FILENAME, newline="") as file:
reader = csv.reader(file)
for row in reader:
names.append(row)
except FileNotFoundError as e:
print("Could not find " + FILENAME + " file.")
sys.exit()
except Exception as e:
print(type(e), e)
sys.exit()
print(names)
if __name__ == "__main__":
main()


Refer to Code Example 8-1. If the names.csv file is not in the same directory as the file that contains the Python code, what type of exception will be thrown and caught?
- OSError
- Exception
- FileNotFoundError
- All of the above