Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Error Handling in Swift 2.0 Error Handling Handling Errors

Austin Hawks
PLUS
Austin Hawks
Courses Plus Student 3,003 Points

I'm learning about errors in Swift, but I don't understand what throwing an error through a fun or catching one does.

I'd like some clarification on the point of a throwing and catching errors in swift. Thanks.

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hey:

Well the concept is simple. If you are working with a function that "throws" and error, because of whatever reason, then you can "catch" that error and simply do something with it.

You may for example try to download something from a website, and this information is not there, and your code requires this information to continue its operation.. There will be an error there and the app may crash. If you implement error handling, then the function that manages the download of this information, can throw and error, and then by catching it, you can let the user know that there was a problem downloading the information. This way the app does not seem to break, rather it may have a temporary issue with its internet connection.

That's one of many examples.

As you go on with the tutorials, you will understand more.

Good luck