exCepTIon handlIng In pyThon – important questions for Boards 2027
CBSE Class 12 Computer Science
What are built-in exceptions in Python?
Answer
Built-in exceptions are exceptions usually defined in the compiler or interpreter for commonly occurring errors in Python.
What is a syntax error, and why is it also called a parsing error? State what must be done before a program with a syntax error can execute successfully.
Answer
A syntax error occurs when the rules of a programming language are not followed while writing a program; it is also called a parsing error. The error must be rectified, and the program saved and rerun before the interpreter executes it.
Explain the roles of the try block, except clause, and optional else clause in exception handling. Also describe what happens to program execution when an exception occurs. (5 marks)
Answer
Exception handling uses try, except and optional else clauses to manage errors and control program execution. 1. The try block contains code where errors or exceptions may occur during execution. Python raises an exception when an error is encountered. 2. Each except clause contains handler code for a matching exception raised in the try block. When an exception occurs, normal program flow is interrupted and control moves to the appropriate exception handler. 3. The optional else clause contains code that executes only if no exception occurs in the try block. 4. If no except clause handles an exception, the exception remains unhandled and is re-raised after the finally block, if present. These clauses help handle exceptions and prevent a program from terminating abruptly when a suitable handler is provided.
Which statement best describes an exception in Python? (A) It is an error that can occur during execution even when the statement is syntactically correct, such as division by zero. (B) It is an error that occurs only when Python cannot parse a statement. (C) It is an error that can occur only before a program starts executing. (D) It is an error that always means the program produced the intended output.
Answer
(A) It is an error that can occur during execution even when the statement is syntactically correct, such as division by zero. — Exceptions can arise during execution even when a statement or expression is syntactically correct.
What does Python’s standard library provide to deal with commonly occurring errors?
Answer
Python’s standard library provides standardized solutions for commonly occurring errors through its extensive collection of built-in exceptions.
What information does the Python interpreter provide when it reports a syntax error? How do exceptions differ from syntax errors in when they arise, and what can they do to normal execution?
Answer locked. Unlock all answers on SmartKalike
(a) What information can a built-in exception handler display, and what should the programmer do next? (2 marks) (b) What are user-defined exceptions? (1 mark) (c) Explain how Python raises exceptions and how program code can forcefully trigger an exception. (2 marks)
Answer locked. Unlock all answers on SmartKalike
Which option correctly describes a raised exception? (A) It is a Python object representing an error; an error during execution is said to raise an exception. (B) It is a syntax rule that prevents Python from creating error objects. (C) It is a correction suggestion displayed before a program is executed. (D) It is a Python object that represents only a user-defined error.
Answer locked. Unlock all answers on SmartKalike
Which statement about a try...except...else structure is correct? (A) An else clause is optional and may be used with a try...except clause. (B) An else clause is compulsory and replaces the except clause. (C) An else clause must always appear before the try block. (D) An else clause cannot be used with exception handling.
Answer locked. Unlock all answers on SmartKalike
What does it mean for an exception to be raised? State why a programmer should anticipate possible exceptions and include suitable handling code.
Answer locked. Unlock all answers on SmartKalike
23 more probable questions in this chapter, every one with a full answer.
Unlock all answers on SmartKalike