Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Error Handling in Swift Error Handling Modeling Errors

Why use enums in the first place?

Sorry if I'm a bit slow, but why create enums that follows the error protocol?

Why not just keep coding the app and when an error pops up you handle it?

It's probably obvious, but I just don't get it.

Thanks!

2 Answers

rory Stevens
rory Stevens
3,840 Points

As I understand it, we code in these kind of "safety nets" where we know something COULD go wrong, to make it safer.

If we only handled errors as we went along, we would run the app, notice something's not working as expected, alter the code until it works, and move on.

But what if when we alter the code to fix it, we create another error by accident. We run the app, see that we fixed the first error, but now something else is not working as expected, or worse yet, the application crashes and we don't even know what exactly it is that is making the app crash. How frustrating.

By including these safety nets as we go and modelling possible errors in enums, we can make a case for every error that could occur and so when this situation above occurs, we won't be left stuck trying to figure out what on earth happened, but instead, we get a message telling us "invalid data in the XYZ box" or "XYZ failed" or, even better, "XYZ failed because of XYZ". Now we know what went wrong, and now we can fix it.

As for why enums specifically? For one thing, the cases can easily model the different errors, but another reason not in this video but that you can find in the apple documentation, is that enumeration cases can have associated values:

Swift enumerations are particularly well suited to modeling a group of related error conditions, with associated values allowing for additional information about the nature of an error to be communicated. For example, here’s how you might represent the error conditions of operating a vending machine inside a game:

enum VendingMachineError: Error {

case invalidSelection

case insufficientFunds(coinsNeeded: Int)

case outOfStock

}

So basically, in this example they've given us, if there were insufficient funds, we would not only know that there were insufficient funds, but also how many coins we were short by because we were able to include associated data about the amount of coins needed because we modelled the error using an enum. The more specific we get, the less we will be confused later on when s**t hits the fan and our app crashes, fails, or works unexpectedly.

Joshua Hardy
Joshua Hardy
17,204 Points

According to the lesson: enums are particularly well suited for the task of conforming error types to the error protocol.

Why not just handle the errors as they come up while building the app? It seems that this goes along the same concept as building tests for your code to ensure it works instead of just running the app and hoping for the best. You set yourself up for a better chance of success by creating enums that follow the error protocol.