• 회원가입
  • 로그인
  • 구글아이디로 로그인

[keyword] Python - finally 키워드 - try 구문 마자막에 무조건 실행. (= 파이널리)

773  
finally 예제 

 

try:

  x > 3 # 에러 유발 (∵ x 변수 비정의)

except:

  print("에러 있음")

else:

  print("에러 없음")

finally:

  print("try...except 구문 종료")

 

결과값:

에러 있음

try...except 구문 종료

 

finally 정의

 

(try..except.. 또는 try..except..else) 구문 마지막에 실행할 내용.

※ try 구문의 에러 유무와 상관없이 마지막에 무조건 실행됨.

※ 객체를 닫아 리소스 비울 때 유용.

 

 

finally 예제 - 객체 닫아 리소스 비우기

 

try:

  f = open("hz.txt")

  f.write("Homzzang.com") # 읽기 전용 파일에 쓸 때 에러 발생.

except:

  print("파일에 쓸 때 뭔가 오류 발생.")

finally:

  f.close()


결과값: 파일에 쓸 때 뭔가 오류 발생.

PS.

try...else... 구문 : https://homzzang.com/b/py-32


분류 제목
module Python - math.erf() 메서드 -
module Python - math.erfc() 메서드 -
module Python - math.exp() 메서드 -
module Python - math.expm1() 메서드 -
module Python - math.fabs() 메서드 -
module Python - math.factorial() 메서드 -
module Python - math.floor() 메서드 ★ - 바닥 반올림 (= 하위 정수로 반올림. = floor메…
module Python - math.fmod() 메서드 -
module Python - math.frexp() 메서드 -
module Python - math.fsum() 메서드 -
module Python - math.gamma() 메서드 -
module Python - math.gcd() 메서드 -
module Python - math.hypot() 메서드 -
module Python - math.isclose() 메서드 -
module Python - math.isfinite() 메서드 - 숫자 유한 여부 체크 (= math.isfinite메…
module Python - math.isinf() 메서드 -
module Python - math.isnan() 메서드 ★ - NaN 여부 체크. (= isnan메서드 = 이즈난메서…
module Python - math.isqrt() 메서드 -
module Python - math.ldexp() 메서드 -
module Python - math.lgamma() 메서드 -
20/24
목록
찾아주셔서 감사합니다. Since 2012