PCPP Advanced Exception Handling Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Thames
T
Thames
Community Contributor
Quizzes Created: 11982 | Total Attempts: 187,298
| Questions: 20 | Updated: Aug 15, 2026
Please wait...
Question 1 / 21
🏆 Rank #-- ▾
0 %
0/100
Score 0/100

1. What is the purpose of exception chaining using the `from` keyword?

Explanation

Using `raise NewException(...) from original_exception` explicitly links the new exception to the one that caused it. This preserves the original error context and traceback, making it easier to understand the underlying cause when debugging.

Submit
Please wait...
About This Quiz
PCPP Advanced Exception Handling Quiz - Quiz

This quiz evaluates your mastery of advanced exception handling in Python, a core competency for the PCPP certification. You'll test your understanding of custom exceptions, exception hierarchies, context managers, and best practices for robust error management. Perfect for college-level learners preparing for professional Python development.

2.

What first name or nickname would you like us to use?

You may optionally provide this to label your report, leaderboard, or certificate.

2. The `contextlib` module provides the ______ decorator for creating simple context managers.

Explanation

The `contextmanager` decorator lets you create a context manager from a generator function. Code before `yield` runs when entering the `with` block, and code after it runs when the block exits, including when an exception occurs.

Submit

3. In a custom exception class, the `__init__` method should call ______ to properly initialize the base Exception class.

Explanation

In a custom exception, `super().__init__(...)` invokes the parent `Exception` class’s initializer, passing along the message or other arguments. This lets Python store the exception’s arguments correctly and ensures standard behavior—such as displaying the message and supporting `.args`—works as expected, without duplicating the built-in class’s initialization logic.

Submit

4. True or False: The `finally` block executes even if a `return` statement is in the `try` block.

Explanation

A `finally` block runs as control leaves the `try` block, including when a `return` statement is encountered. It executes before the method returns, making it useful for cleanup tasks such as closing files or releasing resources.

Submit

5. Which module provides tools for working with exception tracebacks?

Explanation

The `traceback` module provides utilities for inspecting and displaying exception tracebacks, helping you see where an error occurred and follow the sequence of calls that led to it.

Submit

6. What is the function of `sys.exc_info()` in exception handling?

Explanation

`sys.exc_info()` returns information about the most recently handled exception as a three-part tuple: the exception type, the exception value (instance), and the traceback object. It is commonly used inside an exception handler to inspect or log the exception.

Submit

7. When an exception is caught but you want to suppress it silently, which approach is most appropriate?

Explanation

A handler containing only `pass` intentionally ignores the exception, and catching `Exception` is generally suitable for suppressing ordinary application errors. A bare `except` also works syntactically, but it can catch system-exiting exceptions such as `KeyboardInterrupt` and `SystemExit`, so it should be avoided when possible. Silent suppression should be used sparingly because it can hide bugs.

Submit

8. True or False: You can have multiple `except` blocks for the same `try` block.

Explanation

A `try` block can have multiple `except` blocks to handle different kinds of exceptions separately. When an exception occurs, Python checks the handlers in order and runs the first one that matches the exception type. This lets you provide specific responses for different errors while keeping them in one `try` block.

Submit

9. What does the `__enter__` method in a context manager return?

Explanation

When execution enters a `with` block, Python calls the context manager’s `__enter__` method. Its return value is assigned to the variable after `as`, allowing the block to use a resource or other relevant object. The value need not always be a resource, but it is commonly one.

Submit

10. Which of these is the correct syntax for raising an exception with a custom message?

Explanation

In Python, an exception is raised by naming the exception class and passing its message as an argument in parentheses. `ValueError('Invalid value')` creates the exception with that message, and `raise` signals it. The other options either use invalid syntax or create an exception without raising it.

Submit

11. Which built-in exception class is the base for all non-system-exiting exceptions in Python?

Explanation

In Python, `Exception` is the base class for exceptions that do not signal interpreter or system exit. Most errors intended to be handled by application code inherit from it, while system-exiting exceptions such as `SystemExit` and `KeyboardInterrupt` inherit directly from `BaseException`.

Submit

12. True or False: The `else` block in a try-except statement executes if no exception occurs.

Explanation

In a try-except statement, the `else` block runs when the code in the `try` block completes without raising an exception. If an exception occurs, the matching `except` block runs instead.

Submit

13. Which exception type should you raise when a function receives an argument of inappropriate type?

Explanation

Raise a TypeError when an argument has an inappropriate data type for the function, such as passing a string where an integer is expected. ValueError is generally used when the type is acceptable but the argument’s value is invalid.

Submit

14. When you catch an exception using `except Exception as e`, what does the variable `e` represent?

Explanation

The variable `e` refers to the exception object that was raised. It contains details about the error and can be inspected or used to access its message and other exception attributes; traceback information is available through the exception’s traceback-related attributes.

Submit

15. What is the correct order of exception handling when multiple `except` blocks are used?

Explanation

Exception handlers are checked from top to bottom. Put specific exception types first so they can be caught before a broader parent exception; otherwise, the general handler may catch them first and make the specific handlers unreachable.

Submit

16. Which method must be implemented in a context manager class to define cleanup behavior?

Explanation

`__exit__` defines what a context manager does when execution leaves a `with` block. It is where cleanup actions—such as closing a file or releasing a lock—are typically performed, including when an exception occurs. `__enter__` handles setup when the block begins.

Submit

17. What does the `with` statement primarily accomplish in exception handling?

Explanation

The `with` statement uses a context manager to handle setup and cleanup automatically. This ensures resources, such as files or locks, are properly released even if an exception occurs within the block.

Submit

18. In Python, which keyword allows you to re-raise the current exception?

Explanation

Inside an `except` block, a bare `raise` re-raises the exception currently being handled, preserving its original traceback. `retry` and `rethrow` are not Python keywords, while `raise exception` explicitly raises the specified exception object.

Submit

19. What is the primary purpose of the `finally` block in exception handling?

Explanation

The `finally` block runs after the `try` and any `except` blocks, whether an exception occurred or not. It is commonly used for cleanup tasks, such as closing files or releasing resources.

Submit

20. When creating a custom exception, which class should you inherit from to follow best practices?

Explanation

Custom exceptions should generally inherit from `Exception`, the standard base class for application-level errors. This makes them work naturally with common exception-handling patterns, such as `except Exception`, while avoiding `BaseException`, which also includes process-control events like `KeyboardInterrupt` and `SystemExit`. Use a more specific subclass when it suits the error.

Submit
×
Saved
Thank you for your feedback!
View My Results
Cancel
  • All
    All (20)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is the purpose of exception chaining using the `from` keyword?
The `contextlib` module provides the ______ decorator for creating...
In a custom exception class, the `__init__` method should call ______...
True or False: The `finally` block executes even if a `return`...
Which module provides tools for working with exception tracebacks?
What is the function of `sys.exc_info()` in exception handling?
When an exception is caught but you want to suppress it silently,...
True or False: You can have multiple `except` blocks for the same...
What does the `__enter__` method in a context manager return?
Which of these is the correct syntax for raising an exception with a...
Which built-in exception class is the base for all non-system-exiting...
True or False: The `else` block in a try-except statement executes if...
Which exception type should you raise when a function receives an...
When you catch an exception using `except Exception as e`, what does...
What is the correct order of exception handling when multiple `except`...
Which method must be implemented in a context manager class to define...
What does the `with` statement primarily accomplish in exception...
In Python, which keyword allows you to re-raise the current exception?
What is the primary purpose of the `finally` block in exception...
When creating a custom exception, which class should you inherit from...
play-Mute sad happy unanswered_answer up-hover down-hover success oval cancel Check box square blue
Alert!