๐Ÿ“˜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