Exception Handling in Python · Class 12 · CBSE · Science Computer Science

Explain why specific exception classes should generally be handled before a general exception class such as Exception. Include a short Python example…

Practice question from Exception Handling in Python (Class 12 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

hard · Subjective · Q-291D67

Explain why specific exception classes should generally be handled before a general exception class such as Exception. Include a short Python example showing the correct order.

Answer: Specific exceptions should be handled before general exceptions because Exception can match almost any standard exception and would prevent later specific handlers from being reached. For example: try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero") except Exception: print("Some other error").

Solution

A general handler such as except Exception: can catch a ZeroDivisionError, so placing it first makes later specific handlers ineffective. The specific-to-general order preserves precise error handling while still allowing unexpected exceptions to be handled.

More from Exception Handling in Python

All of Exception Handling in Python

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed