Error Handling In Swift

Updated 26 February 2021

Save

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.

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.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home