Python | 예외 만들기

지현·2021년 1월 11일
0

Python

목록 보기
8/9
post-thumbnail

목표: 직접 예외 만들어보기

class 예외이름(Exception):
def init(self):
super().init('에러메시지')

    def __init__(self):
        super().__init__('3의 배수가 아닙니다.')
 
def three_multiple():
    try:
        x = int(input('3의 배수를 입력하세요: '))
        if x % 3 != 0:                     # x가 3의 배수가 아니면
            raise NotThreeMultipleError    # NotThreeMultipleError 예외를 발생시킴
        print(x)						   # 에러가 없으면 x 출력
    except Exception as e:
        print('예외가 발생했습니다.', e)
 
three_multiple()

0개의 댓글