Python Continue Loop On Error
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 python try except continue pass more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
Python Raise Exception And Continue
Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping syntaxerror: 'continue' not properly in loop each other. Join them; it only takes a minute: Sign up Python: continue iteration of for loop on exception up vote 11 down vote favorite I have a simple for loop in Python that is exiting on exceptions python on error resume next even though the exception block contains a continue. There are still about 10 lines left to read when it hits an IndexError and exits the for loop. What am I missing here? for row in hkx: ##'hkx' are rows being read in from 'csv.open' try: print row[2],row[4] except IndexError, e: print 'Error:',e print 'Row Data:',len(row),row continue ## I thought this would just move on to the next row in 'hkx' (sorry, total Python newbie hereā¦) Thanks
Python Ignore Exception In Loop
in advance! python exception for-loop share|improve this question edited Nov 28 '11 at 8:37 Thanatos 19.6k85498 asked Nov 28 '11 at 8:18 Nathan 70119 11 It should works, are you sure there is not another exception (not only the IndexError) raised ? –Cédric Julien Nov 28 '11 at 8:23 1 @Nathan: Can you please post a complete, minimal example? –Björn Pollex Nov 28 '11 at 8:39 1 Agree with @CédricJulien - could you maybe post the output? –Marcin Nov 28 '11 at 8:39 @Bjorn - there's not much else to it. These 3 lines precede the for loop: wrhk = csv.reader(open(WRHKcsv,'rbU')) hkx = csv.reader(open(HKXcsv,'rb')) wrhk24 = set((row[2],row[4]) for row in wrhk) & there's a code section that uses xlrd to convert an xls file to csv. –Nathan Nov 28 '11 at 16:43 @Marcin - that's the only error it tells me about. I added the output in the comments below. –Nathan Nov 28 '11 at 16:45 add a comment| 1 Answer 1 active oldest votes up vote 7 down vote It does exactly as it should and continues with the next line. If an exception is terminating your code early then it must either not be IndexError, or it must be being thrown from some code outside the try: block. >>> hkx = [ range(5), range(4), range(4), range(5) ] >>> fo
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 with pass vs continue python us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow python pass 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 Try Without Except
How to get back to the for loop after exception handling up vote 8 down vote favorite 6 I am ready to run this code but before I want to fix the exception handling: for l in bios: OpenThisLink = url + http://stackoverflow.com/questions/8293086/python-continue-iteration-of-for-loop-on-exception l try: response = urllib2.urlopen(OpenThisLink) except urllib2.HTTPError: pass bio = response.read() item = re.search('(JD)(.*?)(\d+)', bio) .... As suggested here, I added the try...except but now if a page doesn't open I get this error: bio = response.read() NameError: name 'response' is not defined So the program continues to execute. Instead I want it to go back to the for loop and try the next url. I tried break instead of pass but that ends the program. Any suggestions? python exception-handling share|improve this question asked http://stackoverflow.com/questions/1843659/how-to-get-back-to-the-for-loop-after-exception-handling Dec 3 '09 at 22:59 Zeynel 3,3861760113 add a comment| 3 Answers 3 active oldest votes up vote 18 down vote accepted Use continue instead of break. The statement pass is a no-op (meaning that it doesn't do anything). The program just continues to the next statement, which is why you get an error. break exits the loops and continues running from the next statement immediately after the loop. In this case, there are no more statements, which is why your program terminates. continue restarts the loop but with the next item. This is exactly what you want. share|improve this answer edited Dec 3 '09 at 23:15 answered Dec 3 '09 at 23:01 Mark Byers 449k8810961194 1 After an exception is handled it returns to where it was caught, just like most burglers. –jathanism Dec 3 '09 at 23:11 thanks. continue works fine. –Zeynel Dec 3 '09 at 23:36 1 @jathanism: no, it doesn't. If you don't raise it again or something, it continues to the statement after the except block. –drevicko Dec 19 '12 at 2:36 add a comment| up vote 1 down vote Try is actually way more powerful than that. You can use the else block here too: try: stuff except Exception: print "oh no a exception" else: print "oh yay no exception" finally: print "leaving the try block" share|improve this answer answered Dec 3 '09 at 23:34 Jochen Ritzel 58.6k9113149 add a comment| up vote 0 down vote you are getting that er
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta http://stackoverflow.com/questions/28973150/python-continue-for-loop-after-exception 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 with us https://mail.python.org/pipermail/tutor/2005-July/040116.html Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like on error you, helping each other. Join them; it only takes a minute: Sign up Python continue for loop after exception up vote 1 down vote favorite I'm trying to create a new version of a file that excludes NULL bytes. I'm using the code below to attempt this however it's still breaking on the NULL byte. How should python continue loop I structure the for statement and try-catch block to keep going after the exception? import csv input_file = "/data/train.txt" outFileName = "/data/train_no_null.txt" ############################ i_f = open( input_file, 'r' ) reader = csv.reader( i_f , delimiter = '|' ) outFile = open(outFileName, 'wb') mywriter = csv.writer(outFile, delimiter = '|') i_f.seek( 0 ) i = 1 for line in reader: try: i += 1 mywriter.writerow(line) except csv.Error: print('csv choked on line %s' % (i + 1)) pass EDIT: Here's the error message: Traceback (most recent call last): File "20150310_rewrite_csv_wo_NULL.py", line 26, in [ author ] > program, when it runs through its steps and encounters an error, to > log > the error and pick up where it left off and keep going. According > to this > link, 'continue' is allowed within an except or finally: Thats true but only if the try block is inside a loop. Consider: error = True def f(): if error: raise ValueError else: print 'In f()' def g(): print 'In g()' while True: try: f() g() except ValueError: error = False continue continue will cause the *next* iteration of the loop to start. Thus the first time round the error is raised in f() and the code jumps to the except clause and from there back to the top of the loop, effectively missing g() out, then next time through no error is raised so both f() and g() are called. If you really want to ignore the error and move to the next line you have to do a try:except on every line (or function call) try: f() except: pass try: g() except: pass Or put the functions in a list if their parameter lists are null or identical: funcs = [f,g] for func in funcs: try: func() except: continue But all of that's bad practice since who knows what nasties you might be allowing through. Its usually possible to structure code to avoid such horrors! HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld Previous message: [Tutor] try except continue Next message: [Tutor] try except continue Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] More information about the Tutor mailing list