Created
April 26, 2022 04:57
-
-
Save gaeng2y/f89928b6c62bb7c99ace3ab4993cab70 to your computer and use it in GitHub Desktop.
error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum PhoneError: Error { case unknown case batteryLow(batteryLevel: Int) } | |
| throw PhoneError.batteryLow(batteryLevel: 20) | |
| - 던져진 오류를 처리하기 위한 작업이 필요하다 | |
| ```swift | |
| func checkPhoneBatteryStatus(batteryLevel: Int) throws -> String { | |
| guard batteryLevel != -1 else { throw PhoneError.unknown } | |
| guard batteryLevel > 20 else { throw PhoneError.batteryLow(batteryLevel: batteryLevel) } | |
| return "배터리 상태 정상" | |
| } | |
| // do - catch example | |
| do { | |
| try checkPhoneBatteryStatus(batteryLevel: -1) | |
| } catch PhoneError.unknown { | |
| print("Unknown") | |
| } catch PhoneError.batteryLow(let batteryLevel) { | |
| print("Battery power shortage: \(batteryLevel)%") | |
| } catch { | |
| print("Etc error: \(error)") | |
| } | |
| // try? / try! -> 동작하던 코드가 에러를 던지면 return 값 nil / try!는 에러를 던지지않는다고 확실할 때 사용 (근데 왜 try 씀?) | |
| let status = try? checkPhoneBatteryStatus(batteryLevel: -1) | |
| let status = try! checkPhoneBatteryStatus(batteryLevel: -1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/HackPeel/hackpeel-22
https://github.com/HackPeel/hackpeel-24