daily pastebin goal
21%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from time import sleep
  2.  
  3. def connect(server):
  4.     print("Attempting to connecting to", server)
  5.     sleep(2)
  6.     raise ConnectionRefusedError("Couldn't connect")
  7.     print("Connected to", server)
  8.  
  9. try:
  10.     conn = connect('main-server')
  11. except ConnectionRefusedError as connection_refused:
  12.     # connection_refused.handled = True
  13.     conn = connect('backup-server')
  14.    
  15. Traceback (most recent call last):
  16.   File "<pyshell#37>", line 2, in <module>
  17.     conn = connect('main-server')
  18.   File "<pyshell#26>", line 2, in connect
  19.     raise ConnectionRefusedError("Couldn't connect")
  20. ConnectionRefusedError: Couldn't connect
  21.  
  22. During handling of the above exception, another exception occurred:
  23.  
  24. Traceback (most recent call last):
  25.   File "<pyshell#37>", line 4, in <module>
  26.     conn = connect('backup-server')
  27.   File "<pyshell#26>", line 2, in connect
  28.     raise ConnectionRefusedError("Couldn't connect")
  29. ConnectionRefusedError: Couldn't connect
  30.    
  31. try:
  32.     conn = connect('main-server')
  33. except ConnectionRefusedError:
  34.     conn = None
  35.  
  36. if not conn:
  37.     conn = connect('backup-server')
  38.    
  39. for server in ('main-server', 'backup-server'):
  40.     try:
  41.         conn = connect(server)
  42.         break
  43.     except ConnectionRefusedError:
  44.         continue
  45. else:
  46.     raise ConnectionRefusedError("Couldn't connect to any server")
  47.    
  48. try:
  49.     conn = connect('main-server')
  50. except ConnectionRefusedError:
  51.     try:
  52.         conn = connect('backup-server')
  53.     except ConnectionRefusedError as nested:
  54.         raise nested from None
  55.    
  56. except ConnectionRefusedError as connection_refused:
  57.     # connection_refused.handled = True
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top