Core9X
Log inSign up

MBOSE Class XII Computer Science · Chapter 1

Exception Handling in Python

Opening soon

Learn to deal with errors in Python programs using try, except, else and finally, so your code fails gracefully.

14 lessons · 56 practice questions · unit "Computational Thinking and Programming" carries 40 marks

See classes open nowLog inMBOSE Class XII opens to new students soon.

What this chapter covers

  • Syntax errors
  • Exceptions
  • Built-in exceptions
  • Raising exceptions — raise and assert
  • Handling exceptions — need, process, try…except, multiple except clauses, except without an exception name, try…except…else
  • Finally clause

Key terms

Syntax error

Breaking the grammar rules of the language

Exception

Error object raised during execution

Built-in exception

Exception predefined in Python

Throwing

Creating an exception object and passing it to the runtime system

Catching

Executing the handler meant for an exception

Exception handler

Code that deals with an exception

raise

Statement that forcefully throws an exception

assert

Statement that raises AssertionError when a condition is false

finally

Block that always runs

Call stack

List of functions searched for a handler

Important questions with answers

10 of the 56 questions on this chapter, solved.

  1. Question 1 · Multiple choice · 1 mark

    Errors caused by breaking the grammar rules of a language are

    • (a) syntax errors
    • (b) logical errors
    • (c) runtime errors
    • (d) semantic errors

    Answer: (a) syntax errors

    Also called parsing errors.

  2. Question 2 · Multiple choice · 1 mark

    import maths raises

    • (a) NameError
    • (b) IOError
    • (c) IndexError
    • (d) ImportError

    Answer: (d) ImportError

    The module cannot be found.

  3. Question 3 · Multiple choice · 1 mark

    The list of functions searched for an exception handler is the

    • (a) call stack
    • (b) queue
    • (c) heap
    • (d) traceback file

    Answer: (a) call stack

    The search goes back through the callers.

  4. Question 4 · Multiple choice · 1 mark

    The else block of a try statement runs when

    • (a) no exception occurs in the try block
    • (b) an exception occurs
    • (c) always
    • (d) never

    Answer: (a) no exception occurs in the try block

    It holds code for the error-free case.

  5. Question 5 · Assertion and reason · 1 mark

    Assertion (A): 10/0 causes an error although its syntax is correct.

    Reason (R): ZeroDivisionError is raised during execution.

    • (a) Both A and R are true, and R is the correct explanation of A.
    • (b) Both A and R are true, but R is not the correct explanation of A.
    • (c) A is true, but R is false.
    • (d) A is false, but R is true.

    Answer: (a) Both A and R are true, and R is the correct explanation of A.

    It is an exception, not a syntax error.

  6. Question 6 · Short answer · 2 marks

    Distinguish a syntax error and an exception.

    Answer

    A syntax error breaks the language rules and stops the program from running; an exception occurs while a correct program runs.

  7. Question 7 · Short answer · 2 marks

    What is the call stack?

    Answer

    The list of functions (in reverse calling order) searched for an exception handler.

  8. Question 8 · Short answer · 3 marks

    When are KeyboardInterrupt, EOFError and OverflowError raised?

    Answer

    On a user interrupt; when input() reaches end of file; when a result exceeds the numeric limit.

  9. Question 9 · Long answer · 5 marks

    Explain any eight built-in exceptions with examples.

    Answer

    Section 1.3; Section 8.2 (HSSLC 2026, Q30(a)).

  10. Question 10 · Long answer · 5 marks

    Explain try, except, else and finally with one program.

    Answer

    Section 1.5; Section 8.2 (HSSLC 2026, Q30(b)).

Study the whole chapter

14 step-by-step lessons, all 56 practice questions with hints, flashcards and mock tests for MBOSE Class XII Computer Science.

See classes open nowLog inMBOSE Class XII opens to new students soon.