Handling Division by Zero in Python Function

  • Share this:

Code introduction


Define a function that attempts to perform division. If the divisor is zero, it catches the exception and returns an error message.


Technology Stack : Exception handling (try-except)

Code Type : Exception handling

Code Difficulty : Intermediate


                
                    
def aaaaaa(arg1, arg2):
    try:
        return arg1 / arg2
    except ZeroDivisionError:
        return "Cannot divide by zero"