Exception Handling in Python · Class 12 · CBSE · Science Computer Science

What is printed by the following Python code? try: try: int("x") except ValueError: raise RuntimeError("bad input") except RuntimeError as error: pri…

Practice question from Exception Handling in Python (Class 12 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

medium · MCQ · Q-6E2DC5

What is printed by the following Python code? try: try: int("x") except ValueError: raise RuntimeError("bad input") except RuntimeError as error: print(type(error).name)

  1. A.ValueError
  2. B.RuntimeErrorCorrect answer
  3. C.bad input
  4. D.No output because the inner exception is ignored

Solution

The inner except block catches ValueError and raises a new RuntimeError. The outer except block catches it, and type(error).name evaluates to RuntimeError.

More from Exception Handling in Python

All of Exception Handling in Python

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed