No, you cannot do that. That's just the way Python has its syntax. Once you exit a try-block because of an exception, there is no way back in.
What about a for-loop though?
funcs = do_smth1, do_smth2
for func in funcs:
try:
func()
except Exception:
pass # or you could use 'continue'
Note however that it is considered a bad practice to have a bare except. You should catch for a specific exception instead. I captured for Exception because that's as good as I can do without knowing what exceptions the methods might throw.
No, you cannot do that. That's just the way Python has its syntax. Once you exit a try-block because of an exception, there is no way back in.
What about a for-loop though?
funcs = do_smth1, do_smth2
for func in funcs:
try:
func()
except Exception:
pass # or you could use 'continue'
Note however that it is considered a bad practice to have a bare except. You should catch for a specific exception instead. I captured for Exception because that's as good as I can do without knowing what exceptions the methods might throw.
While the other answers and the accepted one are correct and should be followed in real code, just for completeness and humor, you can try the fuckitpy ( https://github.com/ajalt/fuckitpy ) module.
Your code can be changed to the following:
@fuckitpy
def myfunc():
do_smth1()
do_smth2()
Then calling myfunc() would call do_smth2() even if there is an exception in do_smth1())
Note: Please do not try it in any real code, it is blasphemy
Videos
I want the loop to skip to the next iteration when ValueError is raised. Raising error there is a must. I try to put continue operator in except block but it's wrong. Please help me, thanks
NList = []
def isInt(N):
try:
int(N)
return True
except:
return False
try:
while True:
N = input("Enter an positive interger: ")
if N == 0: break
NList.append(N)
if not isInt(N):
raise ValueError("It's not an interger")
int(N)
if N < 1:
raise ValueError("It's not positive")
except ValueError as e:
print(e)
continueI have a function that loops over a list of IDs and makes an API query for each ID in the list. This list is 600ish IDs long. Occasionally I run into the issue where my API token will expire before the function completes. In another post I believe i addressed that issue by creating a function that should ensure my API token doesnt expire. What I have is this:
def query_form_dynamic_data(oauth, token) -> list:
# Loop through the list of form IDs and pass the ID as a variable to the GraphQL query
form_id_list = query_pif_id(oauth, token)
dynamic_data = []
try:
token = ensure_token(oauth, token)
transport = create_transport_protocol(token, proxies)
client = create_graphql_client(transport)
for key, val in enumerate(form_id_list):
query = gql(""" query goes here """)
result = client.execute(query, variable_values=val)
result = result["documents"]
dynamic_data.append(result)
except TokenExpiredError as err:
token = ensure_token(oauth, token)
return dynamic_dataWhat im hoping this does is by calling my ensure_token function before the query, if the token has expired, it would refresh the token. But if it expires in the middle of my loop then im screwed. I know it throws the TokenExpiredError if that happens and then it'll rerun my ensure_token function to get a new key, but will the contents of my try block continue at that point?
The tutorial gives a good start, but is not the language reference. Read the reference here.
Note in particular:
The optional else clause is executed if and when control flows off the end of the try clause.
clarified by footnote 2:
Currently, control “flows off the end” except in the case of an exception or the execution of a return, continue, or break statement.
So your use of continue is explicitly addressed by that.
Your code has a continue, so it never gets to the else block. To achieve your result, you can not get to the continue:
Code:
for x in range(1, 10):
try:
if x != 1:
x / 0
except Exception:
print "Kaput:%s" % (x)
else:
print "No exception:%s" % (x)
break
Result:
No exception:1