본문 바로가기

컴퓨터/Python

사용자정의 예외


>>> class NegativeDivisionError(Exception):        << 사용자정의 예외 정의

def __init__(self, value):

self.value = value

>>> def PositiveDivide(a, b):

if(b < 0):

raise NegativeDivisionError(b)       << 제수가 0보다 적은 경우  NegativeDivisionError 발생

return a / b

>>> try:

ret = PositiveDivide(10, -3)

print('10 / 3 = {0}'.format(ret))

except NegativeDivisionError as e:

print("Error - Second argument of PositiveDivide is ", e.value)

except ZeroDivisionError as e:

print("Error - ", e.args[0])

except:

print("Unexpected exception")


Error - Second argument of PositiveDivide is  -3

'컴퓨터 > Python' 카테고리의 다른 글

assert 구문  (0) 2013.07.25
raise 구문  (0) 2013.07.24
예외처리  (0) 2013.07.23
모듈 임포트 파헤치기  (0) 2013.07.22
모듈 임포트  (0) 2013.07.22