You are tasked with creating a function that attempts to perform a division operation and handles any exceptions that may arise. The function should log the error message if an exception occurs and return a specific value based on the type of exception encountered.
safe_divide(numerator, denominator) that takes two parameters: numerator (int) and denominator (int).ZeroDivisionError occurs, log 'Cannot divide by zero' and return None.TypeError occurs (e.g., if the inputs are not integers), log 'Invalid input type' and return None.None.Example 1:
Input: safe_divide(10, 2)
Output: 5.0
Example 2:
Input: safe_divide(10, 0)
Output: None
// Log: 'Cannot divide by zero'
Example 3:
Input: safe_divide(10, 'a')
Output: None
// Log: 'Invalid input type'
safe_divide(10, 2)Output5.0WhyThe division is successful, returning the result of 10 divided by 2.safe_divide(10, 0)OutputNoneWhyA ZeroDivisionError occurs, logging 'Cannot divide by zero' and returning None.safe_divide(10, 'a')OutputNoneWhyA TypeError occurs because the denominator is not an integer, logging 'Invalid input type' and returning None.Numerator and denominator can be any integer.If the denominator is zero, handle the exception appropriately.def safe_divide(numerator, denominator):