Skip to content

Tag: error-handling

Hide traceback unless a debug flag is set

What is the idiomatic python way to hide traceback errors unless a verbose or debug flag is set? Example code: Existing output now, but only desired when called with foo.py –debug: Desired normal output: Here’s a test script: https://gist.github.com/maphew/e3a75c147cca98019cd8 Answer The short way…

Handling a timeout error in Python sockets

I am trying to figure out how to use the try and except to handle a socket timeout. The way I added the socket module was to import everything, but how do I handle exceptions? In the documentation it says you can use socket.timeouterror, but that doesn’t work for me. Also, how would I write the try exce…

In Python try until no error

I have a piece of code in Python that seems to cause an error probabilistically because it is accessing a server and sometimes that server has a 500 internal server error. I want to keep trying until I do not get the error. My solution was: This seems like a hack to me. Is there a more Pythonic way to