Achieving Big Goals

As I write this article, I have a strong sense of accomplishment and sense of pride (hopefully I am not due for a fall). They reason for this is because the writing of this article means that I have…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




A primer on error handling in Swift

Error handling provides a structured mechanism to respond and recover from errors in programs. In this article, we will present our approach to handling errors. Swift provides error processing, such as throwing, catching, and propagating errors as part of the foundation framework. Therefore, all the swift programs can take advantage of the provided mechanism. Although Swift provides Optionals that handle the absence of values, the error handling provides more explanations and also alters the program flow.

In the example below, we present an e-store. The users can load credits and shop from the available inventory.

We define error conditions in an enum called EStoreError. Enums are useful at structuring related errors. To be represented as errors, EStoreError has to conform to the Error protocol. Error protocol provides localizedDescription property to describe the errors. Thus, we use a switch case to describe the error cases.

Throwing an error indicates that something unexpected happened. We throw Errors with throw keyword. For example, the following code throws an error when an item does not exist in the inventory.

When we mark a function/method or initializer with throwskeyword in the function signature, it states that it is a throwing function. Hence, we must handle errors that the function throws.

The shop method propagates any errors it throws, and any code that calls the shop method must either handle the errors. The errors are thrown by guard statements that check certain conditions and throws the appropriate errors. We handle the errors are with docatc and try, try?and try!statements or let them propagate.

In the shopFavorite function, if

throws an error of the EStore enumeration,

handles the error by printing its localized description. Otherwise, all the other general errors propagates fromshopFavoriteFrom to

and we handle the errors here.

For a more concise handling approach, try?lets us convert errors to optionals, by returning nil. In this way, there is no requirement to have do-catch. The example above shows an erroneous and a valid order response by using try? against nil.

If we want to disable error propagation, we use try!. Be careful with this approach because when an error is thrown, try!will let the program crash. The above erroneous try!statement crashes our program.

In this article, we presented a sample application showing error propagation and various handling mechanisms. That’s all for error handling in Swift. Happy programming!

Add a comment

Related posts:

How Do Photographers Prepare For a Wedding Shoot?

Anybody who has chosen to make living as a wedding photographer must have experienced stress at some point of time. However dealing with it just before the wedding is harder than it appears. Shooting…