In Swift, there are many ways to handle errors. In this article, I am going to deeply explain how to handle errors using throwable functions.
If you are familiar with JSONDecoder(), then you have seen the below code before.
do {
try JSONDecoder().decode(MyStruct.self, from: someData)
} catch let error {
print(error)
}
This is called Do-Catch block, where we are forced to use it otherwise Xcode will complain and won’t compile and gives us this error:
“Call can throw, but it is not marked with ‘try’ and the error is not handled”
That’s because the method decode(_:) is a throwable which means it might throw an error and we can’t handle it like a normal function.
To have a better understanding of the difference between try, try!, and try? let’s design our own throwable method.
Say we have to design a system that allows people to ride a Roller Coaster and our requirements are that the rider cannot be shorter than 120 cm and not taller than 210 cm.
First thing we do we need to write an enum and conform it to the Error protocol.
enum RiderHeightError: Error {
case tooShortToRide
case tooTallToRide
}
Then let’s create a computed property that describes our error cases.
enum RiderHeightError: Error {

case tooShortToRide
case tooTallToRide
var description: String {
    switch self {
    case .tooShortToRide:
        return "You are shorter than 120 cm, try next year."
    case .tooTallToRide:
        return "You are taller than 210 cm, try another game."
    }
}

}
Now we have our enum with the error cases ready, let’s write a throwable function.
func safetyCheck(riderHeight: Int) throws {

if riderHeight < 120 {

    throw RiderHeightError.tooShortToRide

} else if riderHeight > 210 {
    throw RiderHeightError.tooTallToRide
} else {
    print("You are good to go. Have fun.")
}

}
Now we’ve created a function called safetyCheck(). It has one parameter which is the rider’s height. The function checks twice on the rider’s height to make sure it complies with the safety rules.
You noticed that we used two new keywords “throws” and “throw”. The difference between both is pretty simple.
“throws” means this function is throwable and in order to use it, you have to write try before the function.
“throw” means that something went wrong and a specific error has to be thrown.
Basically there are three ways to call a throwable function.
First way is to write Do-Catch block which is the recommended way to handle the errors.
do {
try safetyCheck(riderHeight: 110)
} catch let riderError {
if let error = riderError as? RiderHeightError {
print(error.description)
}
}
=> You are shorter than 120 cm, try next year.
We see that we caught the error and we also got a clear description of the error. There are many ways to write the catch block. The next example I’ll show another way to do it.
do {
try safetyCheck(riderHeight: 230)
} catch RiderHeightError.tooShortToRide {
print(“Too Short To Ride The Roller Coaster”)
} catch RiderHeightError.tooTallToRide {

print("Too Tall To Ride The Roller Coaster")

}
=> Too Tall To Ride The Roller Coaster
Second way is to use “try?” instead of Do-Catch block. It will reduce a few lines of code, but it won’t give you what exactly went wrong. It will give you nil instead if an error was thrown.
try? safetyCheck(riderHeight: 100)
=> nil

#swift-programming #swift #error #error-handling #ios-app-development #ios

Advanced Error Handling in Swift 5
2.00 GEEK