TIL
07월 26일: Errors thrown from here are not handled because the enclosing catch is not exhaustive
알롱도담쓰
2023. 7. 26. 12:05
에러
Errors thrown from here are not handled because the enclosing catch is not exhaustive
에러 원인
do-catch 구문에서 catch 블록이 모든 가능한 에러를 처리하지 않을 때 발생
에러 해결
enum에 있는 모든 케이스를 다 해줬는데도 not exhaustive 에러가 떠서 뭐지? 했는데 어떤 에러가 throw 될지 모르기 때문에 디폴트 값을 하나 설정해줘야 한다는 것이었다.
// 2개의 에러를 설정해둔 상태
enum UserInfoError: Error {
case idIsEmpty
case isMissMatch
}
func start() {
do {
try checkUserId()
} catch UserInfoError.idIsEmpty {
print("ID가 입력되지 않았습니다")
} catch UserInfoError.isMissMatch {
// 디폴트 에러 하나 catch
} catch {}
//혹은 이렇게도 표시 가능
do {
try checkUserId()
} catch {
switch error {
case UserInfoError.idIsEmpty : ~
case UserInfoError.isMissmatch :
// 디폴트 에러 값 하나 설정
default:
}
}
}
<참고>
https://developer.apple.com/forums/thread/5131
Swift Exhaustive Error Handling | Apple Developer Forums
Hello, I have a situation where Xcode 7 beta is complaining that "Errors thrown from here are not handled because the enclosing catch is not exhaustive." But I'm confused because, to me, it does appear that my cases are exhaustive, covering all of the one
developer.apple.com