Skip to content
QuiznExam

Exception Handling in Python

Test your knowledge of exception handling in Python, including try, except, else, finally, raise, custom exceptions, and common built-in exceptions.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is the primary purpose of exception handling in Python?

    • A. To handle runtime errors gracefully
    • B. To increase the CPU speed
    • C. To define classes only
    • D. To create loops
    Show answer

    A. To handle runtime errors gracefully

    Exception handling allows a program to detect and handle runtime errors without necessarily terminating unexpectedly.

  2. 2. Which block is used to enclose code that may raise an exception?

    • A. try
    • B. catch
    • C. error
    • D. handle
    Show answer

    A. try

    The try block contains code that may raise an exception and is followed by one or more handlers such as except.

  3. 3. Which keyword is used to handle an exception in Python?

    • A. except
    • B. catch
    • C. handle
    • D. error
    Show answer

    A. except

    The except clause defines how specific exceptions or groups of exceptions should be handled.

  4. 4. Which block is executed regardless of whether an exception occurs?

    • A. finally
    • B. always
    • C. default
    • D. cleanup
    Show answer

    A. finally

    The finally block generally executes whether an exception occurs or not and is commonly used for cleanup operations.

  5. 5. Which keyword is used to explicitly raise an exception in Python?

    • A. raise
    • B. throw
    • C. error
    • D. exception
    Show answer

    A. raise

    The raise statement is used to explicitly raise an exception.

  6. 6. Which exception is raised when an integer is divided by zero?

    • A. ZeroDivisionError
    • B. ArithmeticErrorOnly
    • C. ValueError
    • D. DivisionError
    Show answer

    A. ZeroDivisionError

    Python raises ZeroDivisionError when a division or modulo operation is performed with a zero divisor.

  7. 7. Which exception is commonly raised when attempting to access a list index that does not exist?

    • A. IndexError
    • B. KeyError
    • C. ValueError
    • D. LookupErrorOnly
    Show answer

    A. IndexError

    IndexError occurs when a sequence index is outside the valid range.

  8. 8. Which exception is raised when a dictionary key is not found?

    • A. KeyError
    • B. IndexError
    • C. NameError
    • D. LookupKeyError
    Show answer

    A. KeyError

    KeyError is raised when a dictionary is accessed with a key that does not exist.

  9. 9. What is the purpose of the else block in Python exception handling?

    • A. To execute code when no exception occurs in the try block
    • B. To execute code only when an exception occurs
    • C. To replace the except block
    • D. To always terminate the program
    Show answer

    A. To execute code when no exception occurs in the try block

    The else block executes after the try block completes successfully without raising an exception.

  10. 10. Which exception is commonly raised when an operation receives an argument of the correct type but an inappropriate value?

    • A. ValueError
    • B. TypeError
    • C. NameError
    • D. AttributeError
    Show answer

    A. ValueError

    ValueError occurs when an operation receives an argument with an inappropriate value even though the argument has an appropriate type.

  11. 11. Which exception is commonly raised when an operation or function is applied to an inappropriate type?

    • A. TypeError
    • B. ValueError
    • C. KeyError
    • D. IndexError
    Show answer

    A. TypeError

    TypeError is raised when an operation or function is applied to an object of an inappropriate type.

  12. 12. How can you define a custom exception in Python?

    • A. By creating a class that inherits from Exception
    • B. By creating a function named Exception
    • C. By using the custom keyword
    • D. By modifying the built-in Error class
    Show answer

    A. By creating a class that inherits from Exception

    Custom exceptions are typically created by defining a class that derives from Exception or an appropriate subclass.

  13. 13. What does the following code do? try: x = 10 / 0 except ZeroDivisionError: print("Error")

    • A. It prints Error
    • B. It prints 0
    • C. It terminates before reaching the except block
    • D. It prints 10
    Show answer

    A. It prints Error

    The division by zero raises ZeroDivisionError, which is caught by the except clause and causes Error to be printed.

  14. 14. What can be used to inspect information about an exception inside an except block?

    • A. An exception variable using as
    • B. The error() function only
    • C. The exception() keyword
    • D. The inspectError keyword
    Show answer

    A. An exception variable using as

    An exception can be bound to a variable using syntax such as except ValueError as e, allowing its information to be inspected.

  15. 15. Which statement about Python exception handling is correct?

    • A. try, except, else, finally, and raise can be used to manage exceptions
    • B. Python does not support custom exceptions
    • C. The finally block executes only when an exception occurs
    • D. The except block must always handle every possible exception
    Show answer

    A. try, except, else, finally, and raise can be used to manage exceptions

    Python provides try, except, else, and finally for structured exception handling, while raise can explicitly raise exceptions.