2013-02-16 9 views
15

Ho una SyntaxError sul mio eccezione:Cosa c'è di sbagliato con il mio tranne?

try: 
    opts, args = getopt.getopt(sys.argv[1:], 'P:D:H:d:u:p:nvhmJi:c:Ml:TB:', 
      ['host=', 'port=', 'directory=', 'user=', 'password=', 
      'daemon=', 'noauth', 'help', 'verbose', 'mysql', 
      'icounter=', 'config=', 'nolock', 'nomime', 'loglevel', 'noiter', 
      'baseurl=']) 
except getopt.GetoptError, e: 
    print usage 
    print '>>>> ERROR: %s' % str(e) 
    sys.exit(2) 

ottengo l'errore:

File "main.py", line 199 

except getopt.GetoptError, e: 

SyntaxError: invalid syntax 

Qualcuno ha qualche idea?

+1

Provare 'tranne getopt.GetoptError come e'? – cnicutar

+1

Consulta questa discussione relativa allo stesso numero: http://stackoverflow.com/questions/2535760/python-try-except-comma-vs-as-in-except –

risposta

5

la sintassi non è valida per la cattura l'eccezione

Dovreste aver scritto except getopt.GetoptError as e: invece di except getopt.GetoptError, e:

+1

Questa era la sintassi corretta nelle versioni precedenti di Python , anche se. – tripleee

28

Si utilizza python3 e python3 the raise syntax no longer accepts comma-separated arguments.

Uso as invece:

except getopt.GetoptError as e: 

Questa forma è anche retrocompatibile con 2.6 e 2.7.

+0

Ok, grazie! ora funziona =) – lagarkane

+0

Aggiungo che se l'OP vuole codice compatibile con python <2.6 allora dovrebbe usare 'except getopt.GetoptError:' e quindi recuperare le informazioni sull'errore usando 'sys.exc_info()' – Bakuriu

Problemi correlati