C-log

Welcome to Python : 예외처리 본문

📘Python

Welcome to Python : 예외처리

4:Bee 2023. 7. 25. 14:13
728x90

Title Code Console
try-expect

try
:
    4 / 0
except ZeroDivisionError as e:
    print(e)
division by zero
try-finally

try
:
    f = open('foo.txt', 'w')
finally:
    f.close
 
try-else
try:
    age = int(input('나이를 입력하세요: '))
except:
    print('입력이 정확하지 않습니다.')
else:
    if age <= 18:
        print('미성년자는 출입금지 입니다.')
    else:
        print('환영합니다.')
 
pass

try
:
    f = open("나없는파일", 'r')
except FileExistsError:
    pass
 
raise

class
Bird:
    def fly(self):
        raise NotImplementedError

class Eagle(Bird):
    pass

eagle = Eagle()
eagle.fly()
raise NotImplementedError
NotImplementedError
예외 만들기
class MyError(Exception):
    pass


def say_nick(nick):
    if nick == "바보":
        raise MyError()
    print(nick)


say_nick("천사")
say_nick("바보")
천사
raise MyError()
__main__.MyError
728x90

'📘Python' 카테고리의 다른 글

Welcome to Python : 표준 라이브러리  (0) 2023.07.27
Welcome to Python : 내장 함수  (0) 2023.07.25
Welcome to Python : 패키지  (0) 2023.07.24
Welcome to Python : 모듈  (0) 2023.07.24
Welcome to Python : 클래스  (0) 2023.07.21
Comments