Backend
예외처리는 왜필요할까?
두바이유
2021. 4. 14. 22:57
www.youtube.com/watch?v=5OhyI2-03vQ
try:
number = int(input("정수입력"))
print('원의 반지름', number)
print('원의 둘레', 2 * 3.14 * number)
print('원의 넓이', 3.14 * number * number)
except Exception as exception:
if type(exception) == ValueError:
print('값에 문제가있습니다.')
Exception은 모든 예외의 어머니이고
as뒤에 exception은 변수명이다.
if의 필요성
while True:
try:
a=[274,233,12,43,65,100]
number = int(input('정수입력(0~4까지)'))
print(a[number])
break
except Exception as exception:
# print(type(exception))
if type(exception) == ValueError:
print('값에 문제가있습니다.')
elif type(exception) == IndexError:
print('0~4까지 입력해주세요')
while문과 break를 추가해서 제대로 입력할때까지 계속물어볼수있고 맞는대답을할경우 나올수있도록 처리
반응형