Exception Handling Quiz Class 12

🧯 Exception Handling Quiz for Class 12 — Computational Thinking & Programming-2 (Code 083)

Practice 20 CBSE Class 12 Computer Science (Code 083) MCQs on exception handling in Python — the try, except, else, and finally blocks, common built-in exceptions (ZeroDivisionError, IndexError, KeyError, ValueError, FileNotFoundError), the raise statement, and catching multiple exceptions.

This set includes 3 board-style predict-the-output questions with full working code, including a nested try-except trace and a custom raised exception. Every result below has been verified by actually running the code in Python. Tap any question to reveal the answer with a clear explanation.

🧯 Exception Basics🛟 try / except🧹 finally✅ else⚠️ Exception Types🚩 raise⭐ Board-Style Programs
Q1
🧯 Exception Basics

What is the purpose of exception handling in programming?

  1. To create intentional errors
  2. To ignore errors
  3. To gracefully handle unexpected errors
  4. To slow down program execution
Show Answer & Explanation

✅ Answer: (c) To gracefully handle unexpected errors

Exception handling lets a program respond to unexpected errors gracefully — instead of crashing outright, it can catch the problem, show a helpful message, and continue running.

Q2
🛟 try / except

Which keyword is used to start an exception handling block in Python?

  1. catch
  2. try
  3. handle
  4. except
Show Answer & Explanation

✅ Answer: (b) try

try marks the beginning of a block of “risky” code that might raise an exception. (Python uses except, not catch — that’s the keyword used in languages like Java or JavaScript.)

Q3
🛟 try / except

What is the purpose of the ‘except’ block in exception handling?

  1. To specify the type of exception only, without any code
  2. To define the code to be executed when an exception occurs
  3. To ignore exceptions completely
  4. To terminate the program immediately
Show Answer & Explanation

✅ Answer: (b) To define the code to be executed when an exception occurs

The except block contains the code that runs specifically when an exception occurs in the corresponding try block — this is where you handle the problem.

Q4
🛟 try / except

In a try-except block, where exactly should you place the code that might raise an exception?

  1. Inside the indented block directly under the ‘try:’ line
  2. Inside the ‘except’ block
  3. Inside the ‘finally’ block
  4. In a completely separate function, never inside try itself
Show Answer & Explanation

✅ Answer: (a) Inside the indented block directly under the ‘try:’ line

The risky code goes in the indented block right under try: — that’s the only code Python actually monitors for exceptions. (This question is clarified from the original, whose two similarly-worded options — “after the try keyword” and “between the try and except keywords” — both described the same location, creating unnecessary ambiguity.)

Q5
🧹 try / finally

What is the purpose of the ‘finally’ block in exception handling?

  1. To specify the type of exception
  2. To define the code to be executed only when an exception occurs
  3. To ignore exceptions
  4. To define cleanup code that always executes, exception or not
Show Answer & Explanation

✅ Answer: (d) To define cleanup code that always executes, exception or not

The finally block runs no matter what — whether the try block succeeded, an exception was raised and caught, or even if it wasn’t caught at all. It’s ideal for cleanup tasks like closing files.

Q6
⚠️ Exception Types

Which of the following is a genuine built-in exception in Python?

  1. BreakError
  2. SyntaxException
  3. ValueError
  4. ExceptionError
Show Answer & Explanation

✅ Answer: (c) ValueError

ValueError is a real built-in Python exception, raised when a function gets an argument of the right type but an inappropriate value. BreakError, SyntaxException, and ExceptionError are not real Python exception names.

Q7
🛟 try / except

How can you handle multiple exception types in a single ‘except’ block?

  1. By using multiple ‘except’ blocks only
  2. By listing the exception types together in a tuple, e.g. except (TypeError, ValueError):
  3. By using a special ‘handle’ keyword
  4. Multiple exceptions can never be handled in a single except block
Show Answer & Explanation

✅ Answer: (b) By listing the exception types together in a tuple, e.g. except (TypeError, ValueError):

You group the exception types in a tuple inside one except clause: except (TypeError, ValueError): — this single block then catches either type.

Q8
✅ try / else

What is the purpose of the ‘else’ block in exception handling?

  1. To specify the type of exception
  2. To define the code executed when an exception occurs
  3. To ignore exceptions
  4. To define code that runs only when NO exception occurs in the try block
Show Answer & Explanation

✅ Answer: (d) To define code that runs only when NO exception occurs in the try block

The else block runs only if the try block completed without raising any exception — useful for code that should run only on success, separate from the risky code itself.

Q9
⚠️ Exception Types

Which of the following is a classic example of a runtime error that can be caught using exception handling?

  1. SyntaxError
  2. NameError
  3. ValueError
  4. IndentationError
Show Answer & Explanation

✅ Answer: (c) ValueError

ValueError occurs during execution when a value is invalid for an operation (e.g. int("abc")) and is directly catchable with try-except. (SyntaxError and IndentationError occur during parsing, before your code even starts running, so they generally can’t be caught by a surrounding try-except in the same script. NameError is also a genuine runtime error and would be an acceptable answer too.)

Q10
🔢 Execution Order

In exception handling, what is the correct order of block execution when an exception occurs and is caught?

  1. try -> except -> finally
  2. try -> finally -> except
  3. except -> try -> finally
  4. finally -> try -> except
Show Answer & Explanation

✅ Answer: (a) try -> except -> finally

Python first runs the try block until the exception occurs, then jumps to the matching except block to handle it, and finally runs the finally block (if present) no matter what happened.

Q11
⚠️ Exception Types

Which exception is raised when you divide a number by zero in Python?

  1. ValueError
  2. ZeroDivisionError
  3. OverflowError
  4. ArithmeticError
Show Answer & Explanation

✅ Answer: (b) ZeroDivisionError

Python raises a ZeroDivisionError specifically for division by zero, e.g. print(5/0).

Q12
⚠️ Exception Types

Which exception is raised when you try to access a list index that is out of range?

  1. KeyError
  2. ValueError
  3. IndexError
  4. TypeError
Show Answer & Explanation

✅ Answer: (c) IndexError

Accessing an out-of-range position, like L[5] on a 3-item list, raises an IndexError.

Q13
⚠️ Exception Types

Which exception is raised when you try to access a dictionary key that doesn’t exist?

  1. IndexError
  2. KeyError
  3. ValueError
  4. AttributeError
Show Answer & Explanation

✅ Answer: (b) KeyError

Looking up a missing key directly (e.g. d["b"] when "b" isn’t in the dictionary) raises a KeyError. (Using d.get("b") instead avoids this by returning None.)

Q14
⚠️ Exception Types

Which exception is raised when you try to open a file that doesn’t exist on disk?

  1. ValueError
  2. IOError only, never anything else
  3. FileNotFoundError
  4. ImportError
Show Answer & Explanation

✅ Answer: (c) FileNotFoundError

Attempting to open a non-existent file (e.g. open("missing.txt")) raises a FileNotFoundError — a more specific subclass of OSError.

Q15
🚩 raise Statement

Which keyword is used to manually trigger (raise) an exception in your own code?

  1. throw
  2. raise
  3. error
  4. signal
Show Answer & Explanation

✅ Answer: (b) raise

Python uses raise to deliberately trigger an exception, e.g. raise ValueError("Invalid input"). (throw is the equivalent keyword in languages like Java or C++, not Python.)

Q16
🔍 Accessing Exceptions

How can you access the exception object (and its message) inside an except block?

  1. It’s not possible to access any details about the exception
  2. Using the ‘as’ keyword, e.g. except ValueError as e:
  3. By calling a function named getException()
  4. By checking a global variable named LAST_ERROR
Show Answer & Explanation

✅ Answer: (b) Using the ‘as’ keyword, e.g. except ValueError as e:

Writing except ValueError as e: binds the exception object to the name e, letting you inspect it — e.g. str(e) gives you the actual error message.

Q17
🛟 try / except

What is a risk of using a bare except: clause (with no exception type specified at all)?

  1. It catches absolutely every exception, including ones you didn’t anticipate, which can hide real bugs
  2. It only catches syntax errors
  3. It causes a SyntaxError immediately
  4. It behaves identically to ‘except Exception as e:’ with no downsides
Show Answer & Explanation

✅ Answer: (a) It catches absolutely every exception, including ones you didn’t anticipate, which can hide real bugs

A bare except: catches everything — even exceptions you never intended to handle, like a typo causing a NameError. This can silently mask real bugs, so it’s generally considered bad practice; catching specific exception types is preferred.

Q18
⭐ Board-Style Programs

What does the following program print, in order?

try:
    x = 10
    y = 0
    print(x / y)
except ZeroDivisionError:
    print("Cannot divide by zero")
else:
    print("Division successful")
finally:
    print("Execution complete")
  1. “Division successful” then “Execution complete”
  2. “Cannot divide by zero” then “Execution complete”
  3. “Cannot divide by zero” then “Division successful” then “Execution complete”
  4. Just “Execution complete”
Show Answer & Explanation

✅ Answer: (b) “Cannot divide by zero” then “Execution complete”

x / y raises a ZeroDivisionError, so the except block runs, printing “Cannot divide by zero”. Since an exception occurred, the else block is skipped entirely. The finally block always runs regardless: “Execution complete”.

Q19
⭐ Board-Style Programs

What does the following nested try-except program print?

try:
    try:
        print(1 / 0)
    except TypeError:
        print("Type error caught inner")
except ZeroDivisionError:
    print("Zero division caught outer")
  1. “Type error caught inner”
  2. “Zero division caught outer”
  3. Both messages print
  4. Nothing prints, the program crashes
Show Answer & Explanation

✅ Answer: (b) “Zero division caught outer”

The inner except TypeError: doesn’t match a ZeroDivisionError, so it doesn’t catch it. The exception propagates outward to the outer try-except, where except ZeroDivisionError: does match, printing “Zero division caught outer”.

Q20
🚩 raise Statement

What does the following program print?

try:
    raise ValueError("Custom error message")
except ValueError as e:
    print("Caught:", e)
  1. “Caught: ValueError”
  2. “Caught: Custom error message”
  3. A ValueError traceback, uncaught
  4. “Custom error message” only, with no “Caught:” prefix
Show Answer & Explanation

✅ Answer: (b) “Caught: Custom error message”

raise ValueError("Custom error message") deliberately triggers a ValueError carrying that text as its message. The matching except ValueError as e: catches it, and printing e displays the message itself: “Caught: Custom error message”.

Jitendra Singh
✔ Verified Educator

Jitendra Singh

Founder of CBSEPython.in

I help CBSE Class 9–12 students learn Python, Information Technology, Artificial Intelligence and Computer Science through easy notes, quizzes, MCQs and sample papers.

Read More About Me →