Complete the code to catch a ZeroDivisionError.
try: result = 10 / 0 except [1]: print("Cannot divide by zero")
The ZeroDivisionError is raised when dividing by zero. Catching it allows the program to handle this error gracefully.
Complete the code to always print 'Done' after the try-except block.
try: x = int('abc') except ValueError: print('Invalid number') finally: [1]('Done')
input instead of print.The finally block runs no matter what. Using print here outputs 'Done' always.
Fix the error in the except clause to catch the correct exception.
try: numbers = [1, 2, 3] print(numbers[5]) except [1]: print('Index out of range')
Accessing an invalid list index raises IndexError. Catching this exception handles the error properly.
Fill both blanks to catch a ValueError and print a message, then always print 'Finished'.
try: num = int('abc') except [1]: print('Conversion failed') finally: [2]('Finished')
input instead of print in finally.The except block catches ValueError from invalid int conversion. The finally block prints 'Finished' always.
Fill all three blanks to catch a KeyError, print the error message, and always print 'End'.
my_dict = {'a': 1}
try:
value = my_dict[[1]]
except [2] as e:
[3](f'Error: {e}')
finally:
print('End')Accessing key 'b' causes a KeyError. Catching it as e allows printing the error message. The finally block prints 'End' always.