What is the output of the following? try: x = 1 / 0 except ZeroDivisionError: x = -1 finally: print(x)

Python Developer Medium

Python Developer — Medium

What is the output of the following? try: x = 1 / 0 except ZeroDivisionError: x = -1 finally: print(x)

Key points

  • The ZeroDivisionError is caught by the except block
  • The value of x is set to -1 in the except block
  • The finally block always executes, printing the value of x
  • The code handles the ZeroDivisionError gracefully

Ready to go further?

Related questions