Function for Safe Division Handling Zero Division Errors

  • Share this:

Code introduction


Define a function that performs division and catches an exception when the divisor is zero, returning an error message in such case.


Technology Stack : Exception handling

Code Type : Mathematical operations and exception handling

Code Difficulty : Intermediate


                
                    
def aaaa(a, b):
    try:
        return a / b
    except ZeroDivisionError:
        return "Cannot divide by zero"