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

Mit Sengupta
Mit Sengupta
13,823 Points

Somebody please help me,

I have got not clue on how to solve this after repeated attempts,

error.swift
enum ParserError: ErrorType {
  case EmptyDictionary
  case InvalidKey
}

struct Parser {
  var data: [String : String?]?

  func parse() throws -> Parser{

  }
}

let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data: data)

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Mit:

Given the fact that you may still a bit lost on how to do error handling, maybe you should re watch a couple of times the course, it may help you.

On your code you have to use "guard" statements.

enum ParserError: ErrorType {
    case EmptyDictionary
    case InvalidKey
}

struct Parser {
    var data: [String : String?]?

    // Here we indicate that this method, can throw an error
    func parse() throws {
   // The guard is used as a way to check if there is an error, and if there is, what type of error is it 
        guard let data = data else {
            throw ParserError.EmptyDictionary
        }

  // This guard checks if there is an error, and it throws the type of error "invalidKey"  
        guard let key = data["someKey"] else {
            throw ParserError.InvalidKey
        }
    }
}

let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data: data)

So basically the first guard checks to see if there is data, if no data exist, then it throws the error "EmptyDictionary", if not then it continues to the next guard, once it gets to that guard, it will check if a key is valid from the data it receives, if its not valid then it will throw the error "InvalidKey".

Good luck

Reed Carson
Reed Carson
8,306 Points

this solution doesn't work

Jhoan Arango
Jhoan Arango
14,575 Points

Reed Carson :

This solution does work, try it. It's meant for the first part of the challenge and not the whole 3 parts of it.

Parsenz Dev
Parsenz Dev
13,198 Points
enum ParserError: ErrorType {
    case EmptyDictionary
    case InvalidKey
}

struct Parser {
    var data: [String : String?]?

    func parse() throws {
        // Guard against empty dictionaries
        // We're using '_' here because we we never use this variable again
        guard let _ = data else {
            throw ParserError.EmptyDictionary
        }

        // Guard against an ommitted key 
        guard let _ = data?["someKey"] else {
            throw ParserError.InvalidKey
        }
    }
}

let data: [String : String?]? = ["someKey": nil]
let parser = Parser(data: data)

// Calling the func and shenanigans
do {
    try parser.parse()
} catch ParserError.EmptyDictionary {
    print("Errr...empty vessels n all, innit?")
} catch ParserError.InvalidKey {
    print("Errr...you might wanna get something sensible in here...")
}