What you actually try to do in this case is to customize the error message. You should re-raise the exception with your custom message
In other words, you should propagate the error from your function and catch it when you call the function.
Of course there might be cases when you just want to catch the error (e.g. in mid loop) print some message and continue the execution of the function/loop
def division1(divideBy): try: return 72 / divideBy except ZeroDivisionError: raise ZeroDivisionError('72 divided by 0 produces an Error: Invalid argument.') for n in (2, 12, 0, 1): try: print(f'72 divided by {n} is {division1(n)}.') except ZeroDivisionError as zde: print(zde)
Output:72 divided by 2 is 36.0.
72 divided by 12 is 6.0.
72 divided by 0 produces an Error: Invalid argument.
72 divided by 1 is 72.0.
of course what you would really do isfor n in (2, 12, 0, 1): try: print(f'72 divided by {n} is {72 / n)}.') except ZeroDivisionError: print ('72 divided by 0 produces an Error: Invalid argument.')If you like/need you may wrap it as function
def divide72(divisor): try: print(f'72 divided by {n} is {72 / divisor)}.') except ZeroDivisionError: print ('72 divided by 0 produces an Error: Invalid argument.') for n in (2, 12, 0, 1): divide72(n)I understand though that this function is just an example
In other words, you should propagate the error from your function and catch it when you call the function.
Of course there might be cases when you just want to catch the error (e.g. in mid loop) print some message and continue the execution of the function/loop
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs