Question 11 pts A Python program should use try statements to handle Flag this Q
ID: 3916093 • Letter: Q
Question
Question 11 pts
A Python program should use try statements to handle
Flag this Question
Question 21 pts
Consider the following code:
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()
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?
Flag this Question
Question 31 pts
Consider the following code:
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()
If the for statement in the try clause refers to readers instead of reader, what type exception will be thrown and caught?
Flag this Question
Question 41 pts
If a program attempts to read from a file that does not exist, which of the following will catch that error?
Flag this Question
Question 51 pts
It’s a common practice to throw your own exceptions to test error handling routines that
Flag this Question
Question 61 pts
The finally clause of a try statement
Flag this Question
Question 71 pts
To cancel the execution of a program in the catch clause of a try statement, you can use the
Flag this Question
Question 81 pts
To throw an exception with Python code, you use the
Flag this Question
Question 91 pts
When an exception is thrown, Python creates an exception object that contains all but one of the following items of information. Which one is it?
Flag this Question
Question 101 pts
Within the try clause of a try statement, you code
all exceptions that might be thrown by a programExplanation / Answer
11)A Python program should use try statements to handle
-- all exceptions that might be thrown by a program
21)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?
-- FileNotFoundError
31)If the for statement in the try clause refers to readers instead of reader, what type exception will be thrown and caught?
-- NameError
41)If a program attempts to read from a file that does not exist, which of the following will catch that error?
-- FileNotFoundError and ValueError
51) It’s a common practice to throw your own exceptions to test error handling routines that
-- provide for many different types of exceptions
61) The finally clause of a try statement
-- is executed whether or not an exception has been thrown
71) To cancel the execution of a program in the catch clause of a try statement, you can use the
-- exit() function of the sys module
81) To throw an exception with Python code, you use the
-- raise statement
91) When an exception is thrown, Python creates an exception object that contains all but one of the following items of information. Which one is it?
-- the severity of the exception
101) Within the try clause of a try statement, you code
-- a block of statements that might cause an exception
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.