ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
class Err(Exception):
    def __init__(self, code):
        super().__init__(f"code {code}")
        self.code = code
try:
    try:
        raise Err(7)
    except Err as e:
        e.code += 1
        raise
except Exception as e:
    print(e.code, e, e.args)
  1. A. 8 code 8 ('code 8',)
  2. B. 7 code 7 ('code 7',)
  3. C. 8 code 7 (7,)
  4. D. 8 code 7 ('code 7',)

Correct answer: D. 8 code 7 ('code 7',)

A bare raise re-raises the same exception object, so the incremented attribute survives. The message and args were fixed when super().__init__ ran with 'code 7'.

More Python Programming questions

Exams that ask this topic