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 trialMert Kahraman
12,118 PointsMultiple Defer Statements
Hi,
I'm confused while using multiple defer statements:
What if the following happens:
function with throws {
if someCondition { defer {someCode1} throw someError1 }
else { defer {someCode2} throw someError2 }
}
In this code, if the function early exits with the throw someError1 line, does only defer {someCode1} is executed? Or both defer statements are executed (with someCode2 first and someCode1 second)?
1 Answer
Krzysztof Growinski
8,405 PointsHi, If the function hits the error your defer statements should be executed (both of them) unless you have a return statement of throw statement in the defer statement.
Check the documentation here https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html