try-catch

Try /Except inside for loop not behaving as expected

Try /Except inside for loop not behaving as expected Question: CODE : def ValidateProxy(LIST_PROXIES): ”’ Checks if scraped proxies allow HTTPS connection ”’ for proxy in LIST_PROXIES: print(‘using’, proxy) host, port = str(proxy).split(“:”) try: resp = requests.get(‘https://amazon.com’, proxies=dict(https=f’socks5://{host}:{port}’), timeout=6) except ConnectionError: print(proxy, ‘REMOVED’) LIST_PROXIES.remove(proxy) print(len(LIST_PROXIES), ‘PROXIES GATHERED’) if len(LIST_PROXIES) != 0: return LIST_PROXIES else: return …

Total answers: 3

How to retry after sql connection failed in python?

How to retry after sql connection failed in python? Question: What’s the right way to retry when connection/write to a DB fails in python? I’m trying to use this code and it works until i restart my sql server and python tries to connect to it i get "retry after 30 sec" for 10 times …

Total answers: 1

Python – Wrong number of arguments exception?

Python – Wrong number of arguments exception? Question: So I have a function like: def my_code(arg1, *args): …. And I want this function to only be able to take either 2 or 3 arguments (that means *args can only be 1 or 2 arguments). How do I throw an error message if the number of …

Total answers: 4

Python: Multiple try except blocks in one?

Python: Multiple try except blocks in one? Question: Is there a neat way to have multiply commands in the try block so that it basically tries every single line without stopping as soon as one command yields an error? Basically I want to replace this: try: command1 except: pass try: command2 except: pass try: command3 …

Total answers: 5

Jump from try to except using python

Jump from try to except using python Question: I searching the way to break the try and go into the except (This example has an syntaxerror in the break line) def fool(): return 0 try: var = fool() if var == 0: pass # from here jump to except except: fool2() Another way i thought …

Total answers: 5

Why is "except: pass" a bad programming practice?

Why is "except: pass" a bad programming practice? Question: I often see comments on other Stack Overflow questions about how the use of except: pass is discouraged. Why is this bad? Sometimes I just don’t care what the errors are and I want to just continue with the code. try: something except: pass Why is …

Total answers: 19

Python check if function exists without running it

Python check if function exists without running it Question: In python how do you check if a function exists without actually running the function (i.e. using try)? I would be testing if it exists in a module. Asked By: Josh Wood || Source Answers: You can use dir to check if a name is in …

Total answers: 7

Using defaultdict to replace try and/or if statements in python

Using defaultdict to replace try and/or if statements in python Question: I have recently found and started using default dictionaries to replace several more bulky constructs. I have read in ‘the zen of python’ that one of the key points of python is “There should be one– and preferably only one –obvious way to do …

Total answers: 3

Ruby equivalent for Python's "try"?

Ruby equivalent for Python's "try"? Question: I’m trying to convert some Python code into Ruby. Is there an equivalent in Ruby to the try statement in Python? Asked By: thatonegirlo || Source Answers: begin some_code rescue handle_error ensure this_code_is_always_executed end Details: http://crodrigues.com/try-catch-finally-equivalent-in-ruby/ Answered By: zengr Use this as an example: begin # "try" block puts …

Total answers: 3