Skip to main contentSkip to solution

Which of the following is correct syntax for handling exception in python?

Solution

✅ Correct Option: 2

Python handles exceptions with a try block followed by an except block. The code that may fail goes under try:, and the except Exception: block runs if that exception occurs.


Options 1, 3 and 4: Python has no catch keyword. It belongs to Java, C++ and JavaScript.

Option 3: raise is used to throw an exception, not to handle one.

else and finally can follow except: else runs if no exception occurs, and finally always runs.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question