AI/Python
Python 예외처리
Urong
2020. 12. 22. 11:41
728x90
다른 언어의 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
반응형