-
Python 예외처리AI/Python 2020. 12. 22. 11:41728x90
다른 언어의 try catch와 같다.
아래 예제는 숫자형이 아닌 문자열을 입력해서 에러가 났을때 숫자 0을 리턴해 주는 예제이다.
Excpetion 은 에러내용을 보여준다.
try: age = int(input("나이 : ")) except Exception as e: age = 0; print(e) print(age)
결과는 다음과 같다.
나이 : a
invalid literal for int() with base 10: 'a'invalid literal for int() with base 10: 'a'
0문자열 a를 입력하면 int형이 아니라는 에러메세지와 함께 숫자 0을 리턴한다.
728x90반응형'AI > Python' 카테고리의 다른 글
Python 으로 경사하강법(Gradient Descent) 구현 (0) 2020.12.23 Python File 읽기 쓰기 기초 (0) 2020.12.18 Python 모듈 기초 random, time (0) 2020.12.18 Python Class 기초 (0) 2020.12.18 Python 함수 Function (0) 2020.12.18