티스토리 뷰
- Exceptions Handling in Python
1. What are Exceptions
Runtime Errors: File Not Found, Divide by Zero, Invalid Input etc.
2. Uncaught Exceptions
3. Python: try -- Except -- else
4. We can catch exception and also get Python Error Message
5. Python Documentation
https://docs.python.org/3/library/exceptions.html
- 에러처리
# 단 하나의 에러에 대해서만 로직으로 처리하는 경우
l = ['apple', 'bannana', 'pear']
if 'apple' in l:
print('apple is in a list')
else:
print('apple is in not a list')
결과)
apple in a list
# 구조적으로 에러에 대비할 수 있다.
l = ['apple', 'bannana', 'pear']
def IsIn(num):
try:
fruit = l[num]
except Exception as e:
result = 'Index ' + str(num) + ' is not in a list.'
print(e)
else:
result = 'Index ' + str(num) + ' is in a list.'
finally:
print(result)
IsIn(0)
결과)
Index 0 is in a list.
# 어떤 에러가 발생했을까?
l = ['apple', 'bannana', 'pear']
def IsIn(num):
try:
fruit = l[num]
except Exception as e:
result = 'Index ' + str(num) + ' is not in a list.'
print(e)
else:
result = 'Index ' + str(num) + ' is in a list.'
finally:
print(result)
IsIn(3)
결과)
list index out of range
Index 3 is not in a list.
# https://www.python.org/dev/peps/pep-0473/#indexerror
# 좀더 정확하게 원하는 에러를 처리해 보자.
l = ['apple', 'bannana', 'pear']
def IsIn(num):
try:
fruit = l[num]
except IndexError as e:
result = 'Index ' + str(num) + ' is not in a list.'
print(e)
else:
result = 'Index ' + str(num) + ' is in a list.'
finally:
print(result)
IsIn(3)
결과)
list index out of range
Index 3 is not in a list.
# 사용자 정의 에러를 발생시켜 로지컬하게 사용
def whatsYourName():
yourName = 'mike'
# yourName = '111'
if yourName.isalpha():
print('Your name is', yourName)
else:
raise Exception("Enter the alpha character.")try:
whatsYourName()
except Exception as e:
print("Error:", e)
결과)
Your name is mike
def whatsYourName():
# yourName = 'mike'
yourName = '111'
if yourName.isalpha():
print('Your name is', yourName)
else:
raise Exception("Enter the alpha character.")try:
whatsYourName()
except Exception as e:
print("Error:", e)
결과)
Error: Enter the alpha character.
# 사용자 정의 에러를 발생시켜 로지컬하게 사용 #2
def my_func(s):
if s.isdigit():
print('You entered a digit character.')
else:
raise Exception("Enter the number character.")
try:
my_func('a')
except Exception as e:
print(e)
결과)
Enter the number character.
- Exceptions not Errors
# Not all exceptions due to error situation
- KeyboardInterrupt Exception
: User pressed ^C
- SystemExit Exception
: Request termination of interpreter
- StopIteration Exception
: No more objects to iterate over
: Loop completed
- Warning Exceptions
: Warnings not as serious as "real errors"
: Used to indicate deprecation, etc.
: See warnings module documentation
- Total
- 410,766
- Today
- 35
- Yesterday
- 44
- 검은왕자
- www.skilledtesting.com Erkan..
- The Braidy Tester
- Hans Schaefer's home page
- systematic-testing.com
- Software Test & Performance..
- Professional Tester Magazine
- Pairwise Testing
- Methods & Tools - Providing..
- Model-Based Testing Home Page
- Dr. Dobb's Portal
- Jeff Offutt (Professor of So..
- Jeff Tian, Ph.D., P.E., Asso..
- Specialist Group in Software..
- StickyMinds.com
- Software QA/Test Resource Ce..
- AMERICAN SOCIETY FOR QUALITY
- Software Testing and Quality..
- Association for Software Tes..
- 다음 일본어 번역
- 네이버 일본어 번역
- 고재팬 일본어 번역
- The Software Engineering Res..
- QA Testing and Test Tools co..
- Information for people with..
- Testing Blog
- QASTREET.COM
- Bret Pettichord
- Tester Center
- I. M. Testy
- Glossary and Technical FAQs
- 위키독스
- Software Testing Mentor
- Martin Fowler's Bliki
- TestFocus
- Game Developers Blog
- smileson
- nihitk's blog
- The Test Management Guide
- Testing Foundations
- Grove Consultants
- 홍환민 페이지
- Free Video Tutorials about S..
- Sustainable Development
- 해리 로빈슨의 모델 기반 테스..
- 테스팅 써커스
- Home - Erik van Veenendaal
- URL metrics
- 소프트웨어 관리
- 경영학
- 자격증
- 소프트웨어 엔지니어링
- 소프트웨어 개발
- 추정
- 협업
- 경제
- 일정
- 평가모델
- Software Engineering
- 독서법
- 경영
- 책읽기
- 소프트웨어 테스트
- software testing
- 애자일