[python] 파이썬 : 한번만 여러 번 시도

파이썬에서는 except하나의 try문장에 대해 여러 문장 을 가질 수 있습니까? 같은 :

try:
 #something1
 #something2
except ExceptionType1:
 #return xyz
except ExceptionType2:
 #return abc



답변

네 가능합니다.

try:
   ...
except FirstException:
   handle_first_one()

except SecondException:
   handle_second_one()

except (ThirdException, FourthException, FifthException) as e:
   handle_either_of_3rd_4th_or_5th()

except Exception:
   handle_all_other_exceptions()

참조 : http://docs.python.org/tutorial/errors.html

“as”키워드는 나중에 코드에서 오류를보다 철저하게 조사 할 수 있도록 오류를 변수에 지정하는 데 사용됩니다. 또한 트리플 예외 경우에 괄호가 파이썬 3에 필요한 참고이 페이지는 더 많은 정보를 가지고 : (블록 제외) 한 줄의 캐치 여러 예외


답변