python - Atteindre plusieurs exceptions dans une ligne (sauf bloc)

Mots clés : pythonexceptionexception-handlingpython

meilleur 5 Réponses python - Atteindre plusieurs exceptions dans une ligne (sauf bloc)

vote vote

91

except (IDontLikeYouException, YouAreBeingMeanException) as e:     pass 
except (IDontLikeYouException, YouAreBeingMeanException), e:     pass 
vote vote

81

try:     may_raise_specific_errors(): except (SpecificErrorOne, SpecificErrorTwo) as error:     handle(error) # might log or have some other default behavior... 
import sys  try:     mainstuff() except (KeyboardInterrupt, EOFError): # the parens are necessary     sys.exit(0) 
import sys  try:     mainstuff() except (KeyboardInterrupt, EOFError) as err:      print(err)     print(err.args)     sys.exit(0) 
import sys  try:     mainstuff() except (KeyboardInterrupt, EOFError), err: # don't do this in Python 2.6+     print err     print err.args     sys.exit(0) 
try:     do_something() except (IDontLikeYouException, YouAreBeingMeanException) as e:     pass 
from contextlib import suppress  with suppress(IDontLikeYouException, YouAreBeingMeanException):      do_something() 
vote vote

74

except (RuntimeError, TypeError, NameError):     pass 
vote vote

63

#This example code is a technique I use in a library that connects with websites to gather data  ConnectErrs  = (URLError, SSLError, SocketTimeoutError, BadStatusLine, ConnectionResetError)  def connect(url, data):     #do connection and return some data     return(received_data)  def some_function(var_a, var_b, ...):     try: o = connect(url, data)     except ConnectErrs as e:         #do the recovery stuff     blah #do normal stuff you would do if no exception occurred 
vote vote

54

try:    You do your operations here;    ...................... except(Exception1[, Exception2[,...ExceptionN]]]):    If there is any exception from the given exception list,     then execute this block.    ...................... else:    If there is no exception then execute this block.  
try:    You do your operations here;    ...................... except Exception1:     functionname(parameterList) except Exception2:     functionname(parameterList) except Exception3:     functionname(parameterList) else:    If there is no exception then execute this block.   def functionname( parameters ):    //your task..    return [expression] 

Questions similaires