Skip to content
C

Python Interview Questions

Exception Handling Interview Questions

try/except/else/finally, raising and chaining exceptions, and custom exception design.

Question 1: What is exception handling?

Ans

Exception handling lets a program detect and respond to runtime errors using try, except, else, and finally. It keeps error paths separate from normal processing.

Example

python
try: value = int("abc") except ValueError: value = 0 print(value)

Important Point

Catch specific exceptions instead of using a broad `except` that hides unrelated bugs.

Question 2: What is try-except?

Ans

A try block contains code that may raise an exception, while a matching except block handles the exception.

Example

python
try: x = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero")

Important Point

Only exceptions raised during the protected try block are handled by its except clauses.

Question 3: What is finally?

Ans

finally contains cleanup code that normally runs whether the operation succeeds or an exception is handled.

Example

python
try: print("work") finally: print("cleanup")

Important Point

For files and similar resources, context managers are often cleaner than manual finally cleanup.

Question 4: What is else in exception handling?

Ans

The else block runs when the try block completes without raising an exception.

Example

python
try: value = int("25") except ValueError: print("Invalid") else: print(value * 2)

Important Point

Putting success-only code in else avoids accidentally treating exceptions from that code as if they came from the protected operation.

Question 5: How do you raise an exception?

Ans

Use raise to deliberately signal an error condition with an exception object or type.

Example

python
def withdraw(balance, amount): if amount > balance: raise ValueError("Insufficient balance") return balance - amount

Important Point

Raise an exception type that accurately describes the invalid condition.

Question 6: How do you create a custom exception?

Ans

Create a class derived from an appropriate built-in exception, usually Exception or a more specific subclass.

Example

python
class InvalidAgeError(ValueError): pass raise InvalidAgeError("Age must be positive")

Important Point

Custom exceptions make domain-specific failure cases easier for callers to catch and understand.

Question 7: What is exception chaining?

Ans

Exception chaining records that one exception was caused while handling another, commonly using raise NewError(...) from original.

Example

python
try: int("abc") except ValueError as exc: raise RuntimeError("Input parsing failed") from exc

Important Point

Chaining preserves the original cause and makes debugging easier.

Question 8: Why avoid bare except?

Ans

A bare except: catches almost everything, including exceptions such as KeyboardInterrupt and SystemExit that applications often should not swallow.

Example

python
try: work() except ValueError as exc: print(exc)

Important Point

Catch the narrowest exception you can meaningfully handle.

Continue Your Preparation