프리렉 - FREELEC
http://freelec.co.kr/book/catalogue_view.asp?UID=134
이강성저
try:
price = selected
except NameError as err:
price = 0
print (price)
2번 문제는 파이썬3에 적절하지 않은 문제라 답변을 생략합니다.
다음과 같이 사용자 예외를 정의하고 예외가 발생했을 때 처리할 수 있습니다. 예외 클래스는 super()를 이용해서 초기화 할 수 있으며, 이 때 전달된 인수들은 인스턴스의 args 속성 값으로 확인이 가능합니다.
class MyException(Exception):
def __init__(self, message, dur):
super().__init__(message, dur)
def __str__(self):
return '{} - {}'.format(self.__class__.__name__, self.args)
try:
raise MyException('msg', 10)
except MyException as a:
print(a.args)
print (a)
import traceback
try:
a = noname
except NameError as err:
print('ready to save the error message to traceback_msg.txt')
s = traceback.print_exc(file=open('traceback_msg.txt', 'w'))
!cat traceback_msg.txt