exception handling - How to raise a warning in Python without stopping (interrupting) the program? -
i dealing problem how raise warning in python without having let program crash / stop / interrupt.
i use following simple function checks if user passed non-zero number. if user passes zero, program should warn user, continue normally. should work following code, should use class warning(), error() or exception() instead of printing warning out manually.
def iszero( i): if != 0: print "ok" else: print "warning: input 0!" return if use code below , pass 0 function, program crashes , value never returned. instead, want program continue , inform user passed 0 function.
def iszero( i): if != 0: print "ok" else: raise warning("the input 0!") return the point want able test warning has been thrown testing unittest. if print message out, not able test assertraises in unittest.
thank you, tomas
you shouldn't raise warning, should using warnings module. raising you're generating error, rather warning.
Comments
Post a Comment