Complete the code to catch all exceptions using the base exception class.
try: x = 1 / 0 except [1]: print("Caught an exception")
The base class for all exceptions is BaseException. Catching it will catch all exceptions.
Complete the code to catch only errors related to arithmetic operations.
try: y = 5 / 0 except [1]: print("Arithmetic error caught")
ArithmeticError is the base class for errors like division by zero and overflow errors.
Fix the error in the code to catch only division by zero exceptions.
try: z = 10 / 0 except [1]: print("Division by zero caught")
ZeroDivisionError is the specific exception raised when dividing by zero.
Fill both blanks to create a custom exception class inheriting from the correct base class.
class MyError([1]): def __init__(self, message): super().__init__([2])
Custom exceptions usually inherit from Exception. The super().__init__ call passes the message to the base class.
Fill all three blanks to catch multiple exceptions in one except block.
try: a = int('abc') b = 1 / 0 except ([1], [2], [3]): print("Caught a value or arithmetic error")
The code catches ValueError from int conversion, ZeroDivisionError from division by zero, and ArithmeticError as a general arithmetic error.