👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I am trying to raise a Warning in Python without making the program crash / stop / interrupt.
I use the following simple function to check if the user passed a non-zero number to it. If so, the program should warn them, but continue as per normal. It should work like the code below, but should use class
Exception() instead of printing the warning out manually.
def is_zero(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 is_zero(i): if i != 0: print "OK" else: raise Warning("the input is 0!") return i
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.
👻 Read also: what is the best laptop for engineering students?