Hi, I am dealing with a problem how to raise a Warning in Python without having to let the program crash / stop / interrupt.
I use following simple function that only checks if the user passed to it a non-zero number. If the user passes a zero, the program should warn the user, but continue normally. It should work like the following code, but should use class Warning(), Error() or Exception() instead of printing the warning out manually.
def isZero( i):
if i != 0:
print "OK"
else:
print "WARNING: the input is 0!"
return i
If I use the code below and pass 0 to the function, the program crashes and the value is never returned. Instead, I want the program to continue normally and just inform the user that he passed 0 to the function.
def isZero( i):
if i != 0:
print "OK"
else:
raise Warning("the input is 0!")
return i
The point is that I want to be able to test that a warning has been thrown testing it by unittest. If I simply print the message out, I am not able to test it with assertRaises in unittest.
Thank you, Tomas