Python3 - 이상

3170 단어 Python3------Base
1. 예외 처리
try:
    print(num)

except NameError as error:
    print("------NameError-------")
    print(error)

except FileNotFoundError:
    print("------FileNotFoundError-------")

except Exception as error:
    print("------Exception-------")
    print(error)

else:
    print(" ")

finally:
    print(" ")

print("------running-------")

2. 사용자 정의 이상
class ShortInputException(Exception):
    ''' '''

    def __init__(self, length, atleast):
        self.length = length
        self.atleast = atleast


def test_error():
    try:
        s = input(" -> ")
        if len(s) < 3:
            # raise 
            raise ShortInputException(len(s), 3)
    except ShortInputException as error:
        print("ShortInputException: %d, %d" \
              %(error.length, error.atleast))

    else:
        print(" ")

test_error()

좋은 웹페이지 즐겨찾기