์ƒˆ์†Œ์‹

๐Ÿ ํŒŒ์ด์ฌ (Python)

ํŒŒ์ด์ฌ(Python) ๊ธฐ์ดˆ (15) EAFP error ์˜ˆ์™ธ์ฒ˜๋ฆฌ Try , except , raise

  • -

 

์ด์ „ ํฌ์ŠคํŒ…์—์„œ ์—๋Ÿฌ์˜ ์ข…๋ฅ˜์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃจ์—ˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์€ EAFP๋ฐฉ์‹์˜ ์ฝ”๋”ฉ์Šคํƒ€์ผ์„ ๊ถŒ์žฅํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

EAFP๋ž€ “It’s Easier to Ask Forgiveness than Permission” 

“ํ—ˆ๋ฝ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค ์šฉ์„œ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ๋‹ค.

์—๋Ÿฌ์— ์•ž์„œ ์ผ๋‹จ ์ˆ˜ํ–‰ ์‹œํ‚ค๊ณ (try), ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ๊ทธ๋•Œ ์ฒ˜๋ฆฌํ•œ๋‹ค(except) ๋ผ๋Š” ๋œป์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

*์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ธฐ๋ณธ๊ตฌ์กฐ

try : ์—๋Ÿฌ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ์ฝ”๋“œ ์‹คํ–‰

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

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

    else : (for. if . try) ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์„๋•Œ ์‹คํ–‰

    finally : ์—๋Ÿฌ์˜ ๋ฐœ์ƒ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„์—†์ด ํ•ญ์ƒ ์‹คํ–‰๋˜๋Š” ๊ตฌ๋ฌธ

 

*์˜ˆ์ œ 1 ๊ธฐ๋ณธ ๊ตฌ์กฐ

#์˜ˆ์ œ 1

name = ['nakamura', 'lee','park','tom']

try :
    x  = name.index('lee')

except ValueError:
    print('์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ValueError ๋ฐœ์ƒ')

else :
    print('OK ์—๋Ÿฌ๋ฐœ์ƒ ์•ˆํ•จ!')

์ถœ๋ ฅ>>>>>>>>>>>>>>...OK ์—๋Ÿฌ๋ฐœ์ƒ ์•ˆํ•จ!

 name์ด๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์— lee๋ผ๋Š” ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜๋ฏ€๋กœ, ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ else๋ฌธ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ name.index('ABC')๋ผ๊ณ  ์ž…๋ ฅํ•œ๋‹ค๋ฉด, 'ABC'๋ผ๋Š” ๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ValueError๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ๋”ฐ๋ผ์„œ except๋ฌธ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

*์˜ˆ์ œ 2 Exception

try :
    x = 1
    y = ['a','b']
    print(x+y)
except  Exception : #์–ด๋–ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ• ์ง€ ๋ชจ๋ฅผ๋•Œ๋Š” ๊ณต๋ฐฑ๋˜๋Š” Exception ์œผ๋กœ ๋†”๋‘๋ฉด ๋ชจ๋“  ์—๋Ÿฌ๋ฅผ ์บ์น˜ํ•œ๋‹ค.

    print('์—๋Ÿฌ ๋ฐœ์ƒ!')
else :
    print('OK ์—๋Ÿฌ๋ฐœ์ƒ ์•ˆํ•จ!')

int์™€ ๋ฆฌ์ŠคํŠธ์˜ ์—ฐ์‚ฐ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” Type์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ except ๋’ค์— TypeError๋Œ€์‹ ์—

๊ณต๋ฐฑ ๋˜๋Š” Exception์œผ๋กœ ๋†”๋‘์–ด๋„ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. (๋ชจ๋“  ์—๋Ÿฌ ๋ฐœ์ƒ์‹œ except๋ฌธ ์‹คํ–‰๋จ)

*์˜ˆ์ œ 3 Finally

try :
    x = 1
    y = ['a','b']
    print(x+y)
except  Exception : 

    print('์—๋Ÿฌ ๋ฐœ์ƒ!')
else :
    print('OK ์—๋Ÿฌ๋ฐœ์ƒ ์•ˆํ•จ!')

finally :  #๋ฌด์กฐ๊ฑด ์‹คํ–‰ ์—๋Ÿฌ๋˜ ์•„๋‹ˆ๋˜

    print('์—๋Ÿฌ๊ฐ€ ๋‚˜๋˜ ์•ˆ๋‚˜๋˜ ์ด ๊ธ€์€ ์ถœ๋ ฅ๋จ')

 

*์˜ˆ์ œ 4 alias (as) ์™€ ๋‹ค์ค‘ ์˜ˆ์™ธ์ฒ˜๋ฆฌ

try :
    x = 1
    y = ['a','b']
    print(y.index('AEF'))

except ValueError as l:   #์ˆœ์„œ๊ฐ€ ์ค‘์š”ํ•˜๋‹ค. ๋งˆ์ง€๋ง‰์— Exception์œผ๋กœ ๋†“๋Š”๊ฒŒ ์ข‹๋‹ค. 
             #as๋ฅผ ์ด์šฉํ•˜์—ฌ ์—๋Ÿฌ ๋‚ด์šฉ๋„ ์ถœ๋ ฅ๊ฐ€๋Šฅํ•˜๋‹ค.
    print('๋ฒจ๋ฅ˜ ์—๋Ÿฌ ๋ฐœ์ƒ')
    print('์—๋Ÿฌ๋‚ด์šฉ :',l)     
except IndexError :
    print('์ธ๋ฑ์Šค ์—๋Ÿฌ ๋ฐœ์ƒ')

except Exception:
    print('์•Œ ์ˆ˜ ์—†๋Š” ์—๋Ÿฌ ๋ฐœ์ƒ')

else :
    print('OK ์—๋Ÿฌ๋ฐœ์ƒ ์•ˆํ•จ!')

import ํ• ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ alias๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์ค‘ ์˜ˆ์™ธ์ฒ˜๋ฆฌ์‹œ์—๋Š” ์œ„์—์„œ ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌ๋˜๋ฏ€๋กœ

๋ฐ˜๋“œ์‹œ ๋ฐœ์ƒํ™•๋ฅ  ๋†’์€ ์—๋Ÿฌ๋ฅผ ์œ„์— ๋ฐฐ์น˜ํ•˜๋Š”๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ชจ๋“  ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” Exception๋ฅผ ๋ฐฐ์น˜ํ•œ๋‹ค๋ฉด

์•„๋ž˜ ๊ตฌ๋ฌธ์€ ์žˆ์œผ๋‚˜ ๋งˆ๋‚˜ํ•œ ์กด์žฌ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 

*์˜ˆ์ œ 5 raise ์—๋Ÿฌ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ

try :
    a = 'Apple'
    if a == 'Apple?W' : 
        print('์˜ค์ผ€์ด')
    else : 
        raise ValueError  #ValueError๋ฅผ ์ง์ ‘ ๋ฐœ์ƒ์‹œํ‚ด

except ValueError :
    print('๋ฌธ์ œ๋ฐœ์ƒ')
  

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.