Simple Division Function with Error Handling

  • Share this:

Code introduction


Defined a simple division function that returns an error message if the second argument is zero.


Technology Stack : None, try-except, ValueError, ZeroDivisionError

Code Type : Mathematical operation function

Code Difficulty :


                
                    
def a_func(arg1, arg2):
    try:
        if arg1 is None or arg2 is None:
            raise ValueError("Arguments cannot be None")
        result = arg1 / arg2
        return result
    except ZeroDivisionError:
        return "Cannot divide by zero"