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
jon kelson
5,149 PointsDo try catch or throws ?
Hi what's the difference between the do try catch and throws statement . Can you use either or combine them ? Please can you explain in simple terms .
Many thanks
4 Answers
Tassia Castro
iOS Development Techdegree Student 9,170 PointsHi Jon,
what's the difference between the do try catch and throws statement?
Throws is a keyword in swift used to specify that a function may throws errors.
func createUser() throws {
// add code here
}
do try catch are the keywords used when you are using a function that throws
do {
let user = try createUser() // this function called createUser may throw an error so you have to put it inside **do** and use the keyword **try** before it and also use the key word catch.
} catch let error{
print(error) // this error will be printed if the user cannot be created.
}
Can you use either or combine them?
When you use throws in a function, you will HAVE to use do try catch in some point of your code that is when you use the function that throws.
I also had problems when I was trying to understand that Jon. Hope it got a little bit better now. feel free to ask more.
jon kelson
5,149 PointsHi sorry just one more thing that's bugging me . Why do we use guard statement for possible function errors and then sometimes a throws statement .?
Tassia Castro
iOS Development Techdegree Student 9,170 PointsHi Jon, I truly understand your confusion. My first tip for you is: stop thinking that sometimes you use guard statement and sometimes you use throws statement.
You should think like that: Throws is the ONLY keyword in Swift that you can use to specify that a method may throw an error. You cannot do that with 'guard'
You use guard to check if something works or not. So guard helps you to decide if the function will or not throw an error. Remember that using the keyword 'throws' when creating a function does not mean that your function WILL throw an error, it means that it MAY throw an error.
However, using a guard statement inside the method that "may throw an error" is a great way to actually throw an error because if it gets inside the guard statement, this means that something you were expecting to work didn't work properly.
func createUser(user: String) throws -> user {
guard let userName = user else {
throw an error //if it gets inside the guard statement is because user was probably empty so I could throw an error here. Bear in mind that this is all up to you. You decide if you want to use a guard statement (you could also use if-let statement to handle this but guard is better in this situation) and you decide if you want and when throw an error.
}
}
Seriously, I don't mind at all answering your questions. If you have more questions feel free to ask, I will answer as soon as I can =)
Good luck!!
jon kelson
5,149 PointsThank you very much Tassia for your detailed and clear answer . I think I finally get it !?.
Correct me if I'm wrong but basically ....
- You use a throws key word in the func if you think the function may return an error.
- Then use the guard statement with else ( preferable over " if let "), then with the "throw" key word. An error in curly braces .
- Then you use a do ( put the possible method error and code in ) try, then catch .statement outside curly braces.
So where you wrote your //" add code here " above , you put the guard statement .
Tassia Castro
iOS Development Techdegree Student 9,170 PointsYeah.
You use to create the function the may throw an error
- "You use a throws key word in the func if you think the function may return an error."
- "Then use the guard statement with else ( preferable over " if let "), then with the "throw" key word. An error in curly braces."
Use when you call the function that may throw an error
- "Then you use a do ( put the possible method error and code in ) try, then catch .statement outside curly braces."
Well done ? Keep working hard and things will start to get better.
jon kelson
5,149 PointsOnce again Tassia, Thank you so much for your time and help . Really appreciate it .??
jon kelson
5,149 Pointsjon kelson
5,149 PointsThanks Tassia , that's much clearer now. Thanks for the great answer ?