์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ)

 

 

<์ด์ „ ๊ธ€>

https://silvercoding.tistory.com/42

 

[python ๊ธฐ์ดˆ] 10. ํŒŒ์ผ ์ฝ๊ธฐ , ์“ฐ๊ธฐ (open)

์ฝ”๋“œ - ํŒจ์บ  ์ˆ˜์—… ์ฝ”๋“œ ์ฐธ๊ณ  (ํŒจ์บ  ์ˆ˜์—… ์ •๋ฆฌ) <์ด์ „ ๊ธ€> https://silvercoding.tistory.com/41 https://silvercoding.tistory.com/40 https://silvercoding.tistory.com/39 https://silvercoding.tistory.com/38..

silvercoding.tistory.com

 

 

* ๋ฌธ๋ฒ•์ ์œผ๋กœ ์—๋Ÿฌ๊ฐ€ ์—†์ง€๋งŒ, ์ฝ”๋“œ ์‹คํ–‰ (๋Ÿฐํƒ€์ž„) ํ”„๋กœ์„ธ์Šค์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋„ ์ค‘์š”ํ•˜๋‹ค. 

* (cf) linter : ์ฝ”๋“œ ์Šคํƒ€์ผ, ๋ฌธ๋ฒ• ์ฒดํฌ 

 

 

์˜ค๋Š˜์€ ๋ฐ”๋ณด์ฝ”๋”ฉ์„ ํ•˜๋ฉด์„œ ์˜ค๋ฅ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž! 

 

 

 [ ์˜ˆ์™ธ ์ข…๋ฅ˜ ] 

(1) SyntaxError : ์ž˜๋ชป๋œ ๋ฌธ๋ฒ• 

print('Test)

 SyntaxError: EOL while scanning string literal 

if True
    pass

 SyntaxError: invalid syntax 

x => y

 SyntaxError: invalid syntax 

 

 

(2) NameError : ์ฐธ์กฐ๋ณ€์ˆ˜ ์—†์Œ 

a = 10 
b = 15 

print(c)

 NameError: name 'c' is not defined 

 

 

(3) ZeroDivisionError : 0 ๋‚˜๋ˆ„๊ธฐ ์—๋Ÿฌ 

print(10 / 0)

 ZeroDivisionError: division by zero 

 

 

(4) IndexError : ์ธ๋ฑ์Šค ๋ฒ”์œ„ ์˜ค๋ฒ„ 

x = [10, 20, 30]
print(x[0])
print(x[3]) # ์˜ˆ์™ธ ๋ฐœ์ƒ

 10 

 IndexError: list index out of range 

 

 

(5) KeyError : ์ฃผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ๋ฐœ์ƒ 

dic = {'name': 'Kim', 'Age': 33, 'city': 'Seoul'}
print(dic['hobby'])

 KeyError: 'hobby' 

print(dic.get('hobby'))  # ๊ถŒ์žฅ

 None 

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  None์„ ๋ฐ˜ํ™˜ํ•˜๋Š” getํ•จ์ˆ˜ ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•œ๋‹ค. 

 

 

(6) AttributeError : ๋ชจ๋“ˆ , ํด๋ž˜์Šค์— ์žˆ๋Š” ์ž˜๋ชป๋œ ์†์„ฑ ์‚ฌ์šฉ ์‹œ 

import time
print(time.time())
print(time.month())

 1627887313.5666506 
Traceback (most recent call last):
  File "c:/Users/./Desktop/python_study/python_easy/section10.py", line 45, in <module>
    print(time.month())
 AttributeError: module 'time' has no attribute 'month' 

 

 

(7) ValueError

x = [1, 5, 9]
x.remove(10)

 ValueError: list.remove(x): x not in list 

x.index(10)

 ValueError: 10 is not in list 

 

 

(8) FileNotFoundError 

f = open('test.txt', 'r')  # ์˜ˆ์™ธ ๋ฐœ์ƒ

 FileNotFoundError: [Errno 2] No such file or directory: 'test.txt' 

 

 

(9) TypeError 

x = [1, 2]
y = (1, 2)
z = 'test'

print(x + y)

 TypeError: can only concatenate list (not "tuple") to list 

print(x + z)

 TypeError: can only concatenate list (not "str") to list 

print(x + list(y))

 [1, 2, 1, 2] 

๋ฆฌ์ŠคํŠธ๋Š” ๋ฆฌ์ŠคํŠธ๋ผ๋ฆฌ๋งŒ ํ•ฉ์น  ์ˆ˜ ์žˆ๋‹ค๋Š” ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๊ณ , ์œ„์™€ ๊ฐ™์ด list๋กœ ํ˜•๋ณ€ํ™˜์„ ํ•ด์ฃผ๋ฉด ์—ฐ์‚ฐ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. 

 

 

 

 

 

* ํ•ญ์ƒ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ๊ฒƒ์œผ๋กœ ๊ฐ€์ •ํ•˜๊ณ  ๋จผ์ € ์ฝ”๋”ฉ 

-> ๊ทธ ํ›„ ๋Ÿฐํƒ€์ž„ ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฝ”๋”ฉ ๊ถŒ์žฅ ( EAFP ์ฝ”๋”ฉ ์Šคํƒ€์ผ ) 

 

 

 

 

 

[ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ธฐ๋ณธ ] 

* try : ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ์ฝ”๋“œ ์‹คํ–‰ (์—ฌ๊ธฐ์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋ฉด except๋กœ ๊ฐ„๋‹ค. ) 

* except : ์—๋Ÿฌ๋ช… 

* else : ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ์‹คํ–‰ 

* finally : ํ•ญ์ƒ ์‹คํ–‰ 

 

- ์˜ˆ์ œ 1 

name = ['Kim', 'Lee', 'Park']

try: 
    z = 'Kim'  # Cho : ์˜ˆ์™ธ ๋ฐœ์ƒ 
    x = name.index(z)
    print('{} Found it! in name'.format(z, x + 1))

except ValueError:
    print('Not found it! - Occured ValueError!')
else:
    print('OK! else!')

 Kim Found it! in name 

 OK! else! 

name = ['Kim', 'Lee', 'Park']

try: 
    z = 'Cho' 
    x = name.index(z)
    print('{} Found it! in name'.format(z, x + 1))

except ValueError:
    print('Not found it! - Occured ValueError!')
else:
    print('OK! else!')

 Not found it! - Occured ValueError! 

 

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— except ๋ธ”๋Ÿญ์ด ์‹คํ–‰๋˜์—ˆ๋‹ค. 

 

 

- ์˜ˆ์ œ 2 

try: 
    z = 'jim' 
    x = name.index(z)
    print('{} Found it! in name'.format(z, x + 1))

except:
    print('Not found it! - Occured Error!')
else:
    print('OK! else!')

 Not found it! - Occured Error! 

 

 

- ์˜ˆ์ œ 3 

try: 
    z = 'Kim' 
    x = name.index(z)
    print('{} Found it! in name'.format(z, x + 1))

except:
    print('Not found it! - Occured Error!')
else:
    print('OK! else!')
finally:
    print("finally ok !")

 Kim Found it! in name 
 OK! else! 
 finally ok ! 

finally๋Š” ๋ฌด์กฐ๊ฑด ์‹คํ–‰๋œ๋‹ค. 

 

 

- ์˜ˆ์ œ 4 : ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ํ•˜์ง€ ์•Š์ง€๋งŒ , ๋ฌด์กฐ๊ฑด ์ˆ˜ํ–‰๋˜๋Š” ์ฝ”๋”ฉ ํŒจํ„ด 

try:
    print('try')
finally:
    print("ok finally!!!")

 try 
 ok finally!!! 

 

 

- ์˜ˆ์ œ 5 : ์—ฌ๋Ÿฌ ๊ฐœ์˜ except ๋ธ”๋Ÿญ 

try: 
    z = 'Kim' 
    x = name.index(z)
    print('{} Found it! in name'.format(z, x + 1))
# except Exception:  # ์ด๊ฑธ ์ฒ˜์Œ์— ๋†“์œผ๋ฉด ์•ˆ๋จ ! 
#     print('Not found it! - Occured Error!')
except ValueError:
    print('Not found it! - Occured ValueError!')
except IndexError:
    print('Not found it! - Occured IndexError!')
except Exception:
    print('Not found it! - Occured Error!')
else:
    print('OK! else!')
finally:
    print("finally ok !")

 Kim Found it! in name 
 OK! else! 
 finally ok ! 

์œ„์™€ ๊ฐ™์ด ์—๋Ÿฌ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์žก์•„์ค„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด ๋•Œ Exception์„ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฐ‘์— ์ž์„ธํ•œ ์—๋Ÿฌ๋Š” ๋‚˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์œ„์น˜ ์„ ์ •์„ ์ฃผ์˜ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ( Exception์ด ํฐ ๋ฒ”์œ„์ด๊ธฐ ๋•Œ๋ฌธ ) 

 

 

- ์˜ˆ์ œ 6 

* ์˜ˆ์™ธ ๋ฐœ์ƒ : raise ( raise ํ‚ค์›Œ๋“œ๋กœ ์˜ˆ์™ธ ์ง์ ‘ ๋ฐœ์ƒ ) 

try: 
    a = 'Jim'
    if a == 'Kim':
        print('OK ํ—ˆ๊ฐ€!')
    else:
        raise ValueError
except ValueError:
    print('๋ฌธ์ œ๋ฐœ์ƒ!')
except Exception as f: 
    print(f)
else:
    print('oK!')

 ๋ฌธ์ œ๋ฐœ์ƒ! 

raise๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง์ ‘ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ๋Š” ValueError๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ผ๊ณ  ํ•˜์˜€์œผ๋ฏ€๋กœ except ValueError๋ถ€๋ถ„์ด ์‹คํ–‰๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

+ Recent posts