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

3 Answers

Matt Skelton
Matt Skelton
4,548 Points

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!

hannah echo
hannah echo
2,872 Points

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

Olivier Van hamme
Olivier Van hamme
5,418 Points

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 ?