Error handling in swift is a process of recovering from error conditions in your project.
Swift provides the support to handle the error in your project.
In Swift, we using Error protocol to handle and define the error.
We can create the custom error type using the enum which needs to confirm the Error protocol.
Error handling in swift provides the help to change the program flow in run time when the error occurred.
We will use the swift try-catch block to handle the error in run time.
Swift Try Catch
When you calling any function and if the function has throws keyword. then that mean we need to use the try-catch block to handle the error.
do-> This is the block where we write the code which can throw the error.
try-> try use the front of the function which can throw the error when you call.
catch-> Catch block use when error throw by function then this block responsible for handle the error.
throws-> Throws keyword use when your function will return the error.
throw-> keyword is used for throwing errors from the error type defined.
Error handling example-> Employee age should be greater than 21
-> First we will create the own error type using an enum with Error protocol.
-> Then we will crate one controller for create the function which throw the error.
-> Then we will call the function in viewdidLoad function.
-> We are calling four times same functions with diffrent input. which provide us the error and success resposne according to condition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //MARK:- when age negative do { try CheckEmployeeValidation(age: -20) } catch let error { print("Error:- \(error)") } //MARK:- when age zero do { try CheckEmployeeValidation(age: 0) } catch let error { print("Error:- \(error)") } //MARK:- when age less than twenty do { try CheckEmployeeValidation(age: 19) } catch let error { print("Error:- \(error)") } //MARK:- when age greater than twenty do { try CheckEmployeeValidation(age: 21) } catch let error { print("Error:- \(error)") } } func CheckEmployeeValidation(age: Int = 0) throws { if age < 0 { throw EmployeeAgeError.ageShouldNotBeNegative } else if age == 0 { throw EmployeeAgeError.ageShouldNotBeZero } else if age < 20 { throw EmployeeAgeError.ageShouldBeGreaterThanTwenty } else { print("success:- \(age)") } } } enum EmployeeAgeError: Error { case ageShouldNotBeNegative case ageShouldNotBeZero case ageShouldBeGreaterThanTwenty } ------------------Output------------------------ Error:- ageShouldNotBeNegative Error:- ageShouldNotBeZero Error:- ageShouldBeGreaterThanTwenty success:- 21 |
And thanks for reading this blog, for detail info please click here
For more blog please click here.
So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.