Antoon Pardon wrote: > This is python 3.4 on a debian box > > In the code below, line 36 raises a StopIteration, I would have > thought that this exception would be caught by line 39 but instead > I get a traceback. > > Did I miss something or is this a bug?
Your code structure is try: raise FileNotFoundError except FileNotFoundError: # try ... except FileNotFoundError inside this handler # omitted as it is only a distraction raise StopIteration except StopIteration: ... This will only catch StopIteration-s in the first try block (lines 22 and 23). To handle exceptions in the first except you need another level of try...except, either try: try: raise FileNotFoundError: except FileNotFoundError: raise StopIteration except StopIteration: ... or try: raise FileNotFoundError: except FileNotFoundError: try: raise StopIteration except StopIteration: ... else: ... > > This is the code: > > try: # 21 > filename = os.path.join(lang_path, lang) # 22 > fl = open(filename) # 23 > except FileNotFoundError: # 24 > try: # 25 > lst = lang.split('_') # 26 > prefix = lst[0] + '*' # 27 > try: # 28 > lst[1] = lst[0].upper() # 29 > except IndexError: # 30 > lst.append(lst[0].upper()) # 31 > lang = '_'.join(lst) # 32 > filename = os.path.join(lang_path, lang) # 33 > fl = open(filename) # 34 > except FileNotFoundError: # 35 > lang = next(iglob(os.path.join(lang_path, prefix))) # 36 > filename = os.path.join(lang_path, lang) # 37 > fl = open(filename) # 38 > except StopIteration: # 39 > fl = () # 40 > > This is the traceback: > > Traceback (most recent call last): > File "/home/antoon/src/projecten/richter/translate.py", line 23, in > use_lang > fl = open(filename) > FileNotFoundError: [Errno 2] No such file or directory: > '/home/antoon/src/projecten/richter/locus/nl' > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "/home/antoon/src/projecten/richter/translate.py", line 34, in > use_lang > fl = open(filename) > FileNotFoundError: [Errno 2] No such file or directory: > '/home/antoon/src/projecten/richter/locus/nl_NL' > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "lang_test", line 4, in <module> > use_lang("Nederlands") > File "/home/antoon/src/projecten/richter/translate.py", line 36, in > use_lang > lang = next(iglob(os.path.join(lang_path, prefix))) > StopIteration > -- https://mail.python.org/mailman/listinfo/python-list