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 Error Handling Cleaning Up With Defer

Absence of "throw"

Why isn't there a "throw" in the second guard statement in this example?

Can you provide the code that you are referring to?

Thanks

Jeff

3 Answers

That's a really good spot in my opinion. To my understanding, the guard statement you're referring to should be preceded with the throw keyword.

For reference for anybody interested:

func process(file name: String) throws {
    guard isValidFile(withName: name) else {
        throw ReadError.invalidFile    // <--- throw keyword present
    }

    let file = open(fileName: name)

    guard let line = try file.readLine() else {
        ReadError.unableToReadLine     // <--- throw keyword missing
    }
}

Good vision!

Thanks for providing the code snippet!

throw isn't required here because of the use of the try keyword

it's a typo. It should be there.

I agree. It must be a typo. At 2:02 Pasan is talking about the second throw statement. He literary mentions it:

From the video transcript:

" … Or in the case of the second throw statement, when we immediately hid that … "

Copy pasting this from the transcript, I discovered another typo: hid instead of hit ?