We use error handling with do-try-catch in swift to respond to recoverable errors . It gives us great control over different fault scenarios that might occur in your code , such as user inputting a wrong username and password .

Further we will see in this article :

  • Why “throw” and “catch” errors
  • Why use syntax do-try-catch
  • How you can create your own custom errors types
  • Different useful scenarios for using try? and try!

WHY “throw” and “catch” Errors?

In iOS development , not all errors are bad . Some errors are part of an app’s lifecycle , such as a “Insufficient funds” message when you try to pay with your credit card. These kinds of errors are recoverable. They can be caught, handled ,and responded to appropriately.

Examples :

  • An ATM displays “Incorrect PIN code” when you try to withdraw money
  • Your car shows a “Fuel low” indicator light as you try to start the engine
  • An authentication attempt for an API returns “Wrong username/password”

You can recover from these errors by displaying an alert message, or doing something else. Your credit card gets blocked after 3 failed attempts , for example . Your car can point you to the nearest fuel pump station when you are out of gas . And you can try a difference username and password .

Swift has a class for supporting error handling with the do-try-catch block of code . do-try-catch has the same king of control over your app as if and return .

For example :

With do-try-catch you can handle errors that have been thrown with throw . We’ll get into this syntax later on. What’s interesting for now, is the throwing and catching principle.

Throwing Error in Swift

Suppose this scenario that should result in an error occurse in your code , you can throw an error like this :

if fuel < 1000 {     
     throw RocketError.insufficientFuel 
}

In the above code , the throw keyword is used to throw an error of type RocketError.insufficientFuel when the fuel variable is less than 1000 .

Imagine we are trying to fire a a rocket :

func igniteRockets(fuel:Int, astronauts: Int) throws {
    if fuel < 1000 {
        throw RocketError.insufficientFuel
    }
    else if astronauts < 3 {
        throw RocketError.insufficientAstronauts(needed : 3)
    }
   print("3....2...1...Ignition !!!LIFTOFF!!!!")
}

In the above function igniteRockets(fuel:astronauts:) will only ignite the rockets if fuel is greater or equal to the 1000 and if there are at least 3 astronauts on board . The igniteRoackets(…) function is also marked with the throws keyword. This keyword indicates to whoever calls this function that errors need to be handled. Swift forces us to handle errors (or _rethrow_them), which means you can’t accidentally forget it!

In above code we are using an error type called RoacketError : AS

enum RocketError: Error {
    case insufficientFuel
    case insufficientAstronauts(needed: Int)
    case unknownError
}

#ios #coding #design #programming #technology #handle

Error Handling in iOS Swift
1.25 GEEK