Introduction to Programming Quiz 8

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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

FileNotFoundError

If a program attempts to read from a file that does not exist, which of the following will catch that error? - NameError and OSError - FileNotFoundError and ValueError - FileNotFoundError and NameError - FileNotFoundError and OSError

FileNotFoundError and OSError

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

a block of statements that might cause an exception

A Python program should use try statements to handle ___. - only the exceptions related to file and database I/O - all exceptions that might be thrown by a program - all exceptions that can't be prevented by normal coding techniques - all the exceptions that aren't caused by coding errors

all exceptions that can't be prevented by normal coding techniques

It's a common practice to throw your own exceptions to test error handling routines that ___. - that handle complexities like lists within lists - handle many varieties of input - catch exceptions that are hard to produce otherwise - provide for many different types of exceptions

catch exceptions that are hard to produce otherwise

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 - cancel() function of the sys module - the built-in cancel() function - the built-in exit() function

exit() function of the sys module

To throw an exception with Python code, you use the ___. - throw statement - raise statement - build-in raise() function - built-in throw() function

raise statement

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.")

try: number = float(input("Enter a number: ")) print("Your number is: ", number) except: print("Invalid number.")


Kaugnay na mga set ng pag-aaral

Nervous System, Digestive System, The Respiratory system, Urinary System, Circulatory System, Endocrine System, Muscular System, Skeletal System

View Set

English III: 1. THE USES AND VARIETIES OF ENGLISH

View Set

The Passion and Death of Jesus Christ- Toben

View Set