Error handling in swift

Error handling is the process of responding to and recovering from error conditions in your program. In many situations we may have to deal with errors. Sometimes we also have to let the co-workers know that the program throws error. I will try to explain every aspects of error handling here in this article.

try?

In Swift, errors are represented by values of types that conform to the Error protocol.

func readFiles(path:String) ->String {if fileNotFound {print(“fileNotFound”)} else if fileNotReadable {print(“fileNotReadable”)} else if fileSizeIsTooHigh {print(“fileSizeIsTooHigh”)}return “Data from file”}

Error Handling is just additional way to write an else-if statement to not only deal with the error messages but also respond after them in a separate block.

enum ErrorsToThrow: Error {case fileNotFoundcase fileNotReadablecase fileSizeIsTooHigh}

Design Throwable Function

According to apple docs: To indicate that a function, method, or initializer can throw an error, you write the throws keyword in the function’s declaration after its parameters. A function marked with throws is called a throwing function. If the function specifies a return type, you write the throws keyword before the return arrow (->).

func canThrowErrors() throws -> Stringfunc cannotThrowErrors() -> String
func readFiles(path:String) throws ->String {if fileNotFound {throw ErrorsToThrow.fileNotFound} else if fileNotReadable {throw ErrorsToThrow.fileNotReadable} else if fileSizeIsTooHigh {throw ErrorsToThrow.fileSizeIsTooHigh}return “Data from file”}
do {let dataFromString = try? readFiles(path: “path for file”)} catch ErrorsToThrow.fileNotFound {print(“fileNotFound”)} catch ErrorsToThrow.fileNotReadable {print(“fileNotReadable”)} catch ErrorsToThrow.fileSizeIsTooHigh {print(“fileSizeIsTooHigh”)}

The performance characteristics of a throw statement are comparable to those of a return statement.

Initializers that throws :

enum ErrorsToThrow: Error {case nameIsEmpty}
class Human {var name:String?init(name:String?) throws {guard let name = name else {throw ErrorsToThrow.nameIsEmpty}self.name = name}}
do {let humanObj = try Human(name: nil)} catch ErrorsToThrow.nameIsEmpty {print(“The name is empty.Cannot initialize human”)}// prints The name is empty.Cannot initialize human

try vs try! vs try?

try is only used within a do-catch block. However, try? and try! can be used without it.

let humanObj1 = try? Human(name: “He-man”) // returns Human?let humanObj2 = try? Human(name: nil) // nil (humanObj2 is an optional which can handle nil)
let humanObj1 = try! Human(name: “He-man”) // returns Humanlet humanObj2 = try! Human(name: nil) // nil and the app will crash

Again, avoid using! in most cases since it will break your program.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store