You could try this approach:
def mySubtract(a, b):
if a is None or b is None or a.shape != b.shape:
sys.stderr.write("a and b don't have the same shape!\n")
raise ValueError("a and b don't have same shape!")
return a-b
I wasn't sure what you meant by "return an error", but if you prefer to just return an invalid result rather than raising a ValueError exception, you could instead just return None
or return "a and b don't have the same shape!"
.
Keep in mind that the caller of your routine would need to know what to do with these special error return codes. So in general exception might be a cleaner approach.