๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
iOS/Swift

Swift) Error Handling

by Jiseong 2022. 2. 12.

๐Ÿ“Œ Error Handling

Swift๋Š” Error protocol์„ ํ†ตํ•ด Error handling์„ ์ง€์›ํ•œ๋‹ค.

๐Ÿ˜ˆ Ways to handle Errors

์Šค์œ„ํ”„ํŠธ์˜ ์—๋Ÿฌ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ•์€ ๋„ค๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  • Error ๋ฐœ์ƒ ํ•จ์ˆ˜์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ์ฝ”๋“œ๋กœ ์ „ํŒŒ(๋˜์ง) (thorws)
  • do-catch๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌ
  • Error๋ฅผ ์˜ต์…”๋„๊ฐ’์œผ๋กœ ์ฒ˜๋ฆฌ
  • Error๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ผ๊ณ  ๋‹จ์ •

๐Ÿ‘ฟ Error Represent

Swift์—์„œ, Error๋Š” Error protocol์„ ์ค€์ˆ˜ํ•˜๋Š” ํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

๐Ÿ‘น Error Protocol์€ ๋นˆ ํ”„๋กœํ† ์ฝœ์ด๋‹ค.

Any type that declares conformance to the Error protocol can be used to represent an error in Swift’s error handling system. Because the Error protocol has no requirements of its own, you can declare conformance on any custom type you create.

Example

enum AnythingError: Error {
  case notingExist
}

๐Ÿคก Error ๋ฐœ์ƒ ํ•จ์ˆ˜์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ์ฝ”๋“œ๋กœ ์ „ํŒŒ - thorws

func canThrowErrors() throws -> String

func cannotThrowErrors() -> String

 

throwsํ•จ์ˆ˜๋งŒ์ด ์—๋Ÿฌ๋ฅผ ์ „ํŒŒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

nonthrows ํ•จ์ˆ˜์—์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ๋Š” ์–ด๋–ค ๊ฒƒ์ด๋“  ๋ฐ˜๋“œ์‹œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.

let anything = [1, 2, 3]
func canThrowErrors(anything : Int) throws -> String {
	guard let existAnything = anything[anything] else {
		throw nothingExist
	}
}

์œ„ ํŒŒ๋ผ๋ฏธํ„ฐ๋ช… ์—‰๋ง์ธ ์ฝ”๋“œ์˜ throws -> String์€ thorws๋ฅผ Stringํƒ€์ž…์œผ๋กœ ํ•˜๊ฒ ๋‹ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ํƒ€์ž…์„ ์ง€์ •ํ•œ ๊ฒƒ์ด๋‹ค.

 

guard์˜ ์กฐ๊ฑด์„ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด else์— ์žˆ๋Š” nothingExist error๋ฅผ ๋˜์ง€๊ฒ ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿ‘บ do-catch๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌ

throws ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š”  try๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

do ์ ˆ์—์„œ tryํ•œ ์ฝ”๋“œ๊ฐ€ ์—๋Ÿฌ๋ฅผ ๋˜์ง€๋ฉด, catch๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ error์— ๋งž์ถฐ ์ฒ˜๋ฆฌํ•œ๋‹ค.

๐Ÿ‘พ do-catch์˜ ํ˜•์‹

do {
    try (throws ํ•จ์ˆ˜)
    statements
} catch error๋ช…1 {
    statements
} catch error๋ช…12 where condition {
    statements
} catch {
    statements
}

๐Ÿ‘ฝ Error๋ฅผ ์˜ต์…”๋„๊ฐ’์œผ๋กœ ์ฒ˜๋ฆฌ - try?

try?๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜ต์…”๋„๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•จ์œผ๋กœ์จ error๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

error ๋ฐœ์ƒ์‹œ nil์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , error๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฐ˜ํ™˜ ํƒ€์ž…์€ ์˜ต์…”๋„์ด ๋œ๋‹ค.

if let data = try? getData() {
	return data
}

์œ„ ์ฝ”๋“œ๋Š” getData()์—์„œ error๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด nil์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์ธ if let ๋‚ด๋ถ€๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค.

โ˜ ๏ธ Error๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ผ๊ณ  ๋‹จ์ • - try!

ํ•จ์ˆ˜๋‚˜ ๋ฉ”์†Œ๋“œ์—์„œ error๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ณ ์žˆ์„ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

 

๋ฐ˜ํ™˜ํƒ€์ž…์€ Non-Optional์ด๋‹ค.

 

try!๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ error๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ Runtime error๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

let image = try! loadImg(atPath: "./Resources/mainLogo.jpg")

์œ„ ์ฝ”๋“œ๋Š” loadImg() ํ•จ์ˆ˜์—์„œ error๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ •์ƒ์ ์œผ๋กœ ์ƒ์ˆ˜์— ๊ฐ’์ด ๋“ค์–ด๊ฐ€์ง€๋งŒ, error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด ๋ž˜ํ•‘๋˜์ง€์•Š์€ nil์ด ๋‚˜์˜ค๋ฉด์„œ Runtime error๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ฒŒ ๋œ๋‹ค.

 

๋‹น์—ฐํžˆ ๋‚จ๋ฐœํ•˜๋ฉด ์ข‹์ง€์•Š์€ ๊ฒƒ์ด๋‹ค.


๐Ÿ“– ๊ทธ๋ƒฅ ๋ˆ„๊ตฐ๊ฐ€ ๋ณด๋ฉด์„œ ํ—ท๊ฐˆ๋ ค ํ• ๊นŒ๋ด ์ ๋Š” ๊ฒƒ

ํ•จ์ˆ˜์™€ ๋ฉ”์†Œ๋“œ๋Š” ์–ด๋– ํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ

ํ•จ์ˆ˜์™€ ๋ฉ”์†Œ๋“œ์˜ ์ฐจ์ด๋Š” ๋ฉ”์†Œ๋“œ๋Š” ํด๋ž˜์Šค ๋ฐ ๊ฐ์ฒด์™€ ์—ฐ๊ด€๋˜์–ด ์žˆ๋Š” ํ•จ์ˆ˜๋ผ๋Š” ๊ฒƒ

๋Œ€ํ‘œ์ ์œผ๋กœ ํด๋ž˜์Šค ๋‚ด์— ์„ ์–ธ๋˜์–ด ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฉ”์†Œ๋“œ ๋ผ๊ณ  ์นญํ•จ

์—ฌํŠผ ๋ฉ”์†Œ๋“œ๋„ ํ•จ์ˆ˜๋‹ค.

'iOS > Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Swift) return์— ๋Œ€ํ•œ ์˜๋ฌธ  (0) 2022.02.12
Swift) Switch vs If else  (0) 2022.02.12
ํ•จ์ˆ˜ ์ „๋‹ฌ์ธ์ž๋ช…๊ณผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ช…  (0) 2022.02.12
Swift) Optional Binding  (0) 2022.02.10
[Swift] Closure  (0) 2021.09.24

๋Œ“๊ธ€