์ฝ๋ - ํจ์บ ์์ ์ฝ๋ ์ฐธ๊ณ (ํจ์บ ์์ ์ ๋ฆฌ)
<์ด์ ๊ธ>
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๋ถ๋ถ์ด ์คํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
'ํ๋ก๊ทธ๋๋ฐ ์ธ์ด > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python ๊ธฐ์ด] 13. SQLITE ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๋ (1) - ํ ์ด๋ธ ์์ฑ ๋ฐ ์ฝ์ ์ญ์ (0) | 2021.08.03 |
---|---|
[python ๊ธฐ์ด] 12. ์ธ๋ถ ํ์ผ ์ฒ๋ฆฌ (Excel, CSV ํ์ผ ์ฝ๊ธฐ ๋ฐ ์ฐ๊ธฐ) (0) | 2021.08.03 |
[python ๊ธฐ์ด] 10. ํ์ผ ์ฝ๊ธฐ , ์ฐ๊ธฐ (open) (0) | 2021.08.02 |
[python ๊ธฐ์ด] 9. ๋ชจ๋๊ณผ ํจํค์ง (0) | 2021.08.02 |
[python ๊ธฐ์ด] 8. ํด๋์ค , ์์ , ๋ค์ค์์ (0) | 2021.08.01 |