Unit 5: Exception handling - Subjective Questions

ECAP776 • Practice Questions with Detailed Answers

20 questions

1

Define an exception in Python. Explain how an exception differs from a syntax error.

2

Explain the purpose and execution flow of Python's try and except statements with an example.

3

Describe the roles of the else and finally clauses in exception handling. Illustrate their order of execution.

4

Distinguish between catching multiple exceptions using separate except blocks and catching them using an exception tuple.

5

Explain why the order of multiple except blocks is important when exceptions have an inheritance relationship.

6

What is the significance of capturing an exception object with the as keyword? Demonstrate how useful information can be obtained from it.

7

Compare a bare except clause, except Exception, and an exception-specific handler such as except ValueError.

8

Explain exception propagation through nested function calls. What happens when no matching handler is found?

9

Describe how the raise statement is used to generate exceptions explicitly. Write an example that validates a person's age.

10

What does a bare raise statement do inside an except block? Explain re-raising with an example.

11

Explain exception chaining using raise ... from .... Why is it useful when translating exceptions?

12

Define a custom exception. Describe the steps for creating, raising, and catching one in Python.

13

Design a custom exception hierarchy for a banking application and explain the advantages of using a common base exception.

14

How can a custom exception store additional information? Create an exception for an invalid examination score and explain its attributes.

15

Compare using built-in exception classes with defining custom exception classes. When should each approach be preferred?

16

Analyze the following pattern and explain why placing too much code inside a try block can cause incorrect exception handling: try: value = int(text); save(value) except ValueError: print("Invalid input").

17

Write and explain a Python function that repeatedly asks for two integers, handles invalid input and division by zero, and returns the quotient only after a successful calculation.

18

Explain how exception handling should be applied when working with files. Include catching multiple exceptions and guaranteed cleanup.

19

Discuss the relationship between return, exceptions, and the finally clause. Why is returning from finally considered dangerous?

20

Develop a robust validation function for a percentage value. It must reject inappropriate types, values outside the valid range, and demonstrate a custom exception, exception chaining, and caller-side handling.