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

What is the main purpose of the finally block in Python exception handling?

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

easy · MCQ · Q-4D2F62

What is the main purpose of the finally block in Python exception handling?

  1. A.It executes only when an exception occurs.
  2. B.It executes only when no exception occurs.
  3. C.It executes whether or not an exception occurs.Correct answer
  4. D.It is used only to define custom exceptions.

Solution

The finally block executes after the try and any matching except block, regardless of whether an exception occurs. It is commonly used for cleanup operations such as closing files.

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