Quiz 7: How to work with exceptions
The finally clause of a try statement
is executed whether or not an exception has been thrown
If a program attempts to read from a file that does not exist, which of the following will catch that error?
FileNotFoundError and OSError
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?
NOT try: number = float(input("Enter a number: ")) print("Your number is: ", number) except ValueError: print("Invalid number.")
Within the try clause of a try statement, you code
a block of statements that might cause an exception
A Python program should use try statements to handle
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
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
To throw an exception with Python code, you use the
raise statement
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
Which of the following is the correct way to code a try statement that displays the type and message of the exception that's caught?
try: number = int(input("Enter a number: ")) print("Your number is: ", number) except Exception as e: print(type(e), e)