Python Raise Error And Continue
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads python warning example with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack
Python 3 Warnings
Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign
Python Continue Loop After Exception
up How to raise a warning in Python without stopping (interrupting) the program? up vote 42 down vote favorite 2 I am dealing with a problem how to raise a Warning in Python without having to let the program crash / stop
Pythonwarnings
/ 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 python warnings vs logging 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 python exception-handling error-handling warnings share|improve this question edited Oct 8 '10 at 15:30 asked Oct 8 '10 at 15:01 Tomas Novotny 88231520 How exactly do you want to notify the user? through email or SMS? cause that can be hooked up but you need to be specific. –aaronasterling Oct 8 '10 at 15:05 1 Why don't you just print the message? –sje397 Oct 8 '10 at 15:06 @sje397 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 do that with assertRaises in unittest. –Tomas Novotny Oct 8 '10 at 15:16 add a comment| 2 Answers 2 active oldest votes up vote 43 down vote accepted You shouldn't raise the warning, you should be using warnings module. By raising it you're generating error, rather than warning. share|improve this answer edite
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About python raise warning and continue Us Learn more about Stack Overflow the company Business Learn more about hiring python3 warning developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the python custom warnings Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Python: How to ignore an exception http://stackoverflow.com/questions/3891804/how-to-raise-a-warning-in-python-without-stopping-interrupting-the-program and proceed? [duplicate] up vote 144 down vote favorite 24 This question already has an answer here: Try/Except in Python: How do you properly ignore Exceptions? 10 answers I have a try...except block in my code and When an exception is throw. I really just want to continue with the code because in that case, everything is still able to run just fine. The problem is http://stackoverflow.com/questions/574730/python-how-to-ignore-an-exception-and-proceed if you leave the except: block empty or with a #do nothing, it gives you a syntax error. I can't use continue because its not in a loop. Is there a keyword i can use that tells the code to just keep going? python exception share|improve this question edited Jan 2 '10 at 1:03 Rob 38.9k25885 asked Feb 22 '09 at 11:02 The.Anti.9 11.2k3697147 marked as duplicate by Eric Brown, Siddharth, mishik, zhangyangyu, mdahlman Jul 24 '13 at 4:17 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. add a comment| 4 Answers 4 active oldest votes up vote 237 down vote accepted except: pass share|improve this answer answered Feb 22 '09 at 11:03 Andy Hume 21.1k42440 50 except Exception: pass # important not to swallow other exceptions! –Roger Pate Feb 22 '09 at 16:46 8 @Aaron - I agree, but the question wasn't if this was a good/bad idea –David Feb 23 '09 at 20:05 12 This will catch SystemExit, KeyboardInterrupt and other things that you probably don't want to catch. –FogleBird Jan 2 '10 at 1:13
Pages Local Site Map ------------------------ Rename Page Delete Page ------------------------ ------------------------ Remove Spam Revert to this revision ------------------------ SlideShow User Login Handling Exceptions The simplest way to handle exceptions is with a "try-except" block: 1 (x,y) = (5,0) 2 try: 3 z = x/y 4 except ZeroDivisionError: 5 print "divide by zero" https://wiki.python.org/moin/HandlingExceptions If you wanted to examine the exception from code, you could have: 1 (x,y) http://www.gossamer-threads.com/lists/python/python/808525 = (5,0) 2 try: 3 z = x/y 4 except ZeroDivisionError as e: 5 z = e # representation: " Error: %s
Post #1 of 8 (12268 views) Permalink Is there a way to continue after an exception ? hello, I would like my program to continue on the next line after an uncaught exception, is that possible ? thanks Stef Mientki krister.svanlund at gmail Feb20,2010,3:59PM Post #2 of 8 (12183 views) Permalink Re: Is there a way to continue after an exception ? [In reply to] On Sun, Feb 21, 2010 at 12:52 AM, Stef Mientki