|
|
|
|
|
by smtddr
4673 days ago
|
|
I got a question. Do you think it's okay use exceptions to end what would be otherwise an infinite loop?
Python example: data = ""
socket.settimeout(1.5)
while 1:
try:
data += socket.recv(1024)
except Exception,e:
store_data(data)
break
If this is not okay, what would you change?(And yes, I know there are edge'ish cases were I'd miss some data here) |
|
The fix is to have a specific nonambiguous name for your exception, so that other error conditions still work properly. As examples consider the StopIteration and GeneratorExit exceptions from Python's standard library. (See http://docs.python.org/2/library/exceptions.html for a list of built-in exceptions.)