Bob Lee Course Part 3 (lesson 6)
Back with Bob! The work on optionals has made sense so far and given me a better of understanding of WHY we need them. The if/let and guard/let codes are clearer too. So now we're moving onto Error Handling...this was something I've covered before with Pasan's course on Treehouse but did not do well with it! So it will be interesting to see how it goes this time!
Start Time - 17:17
Error Handling
Bob makes a good point - error handling is daunting at first as you feel as though you should be an expert at debugging to do it!
Example with roller coaster - basically using if/else statements to see what happens to someone's height if they can go on a roller coaster or not.
Problem with using else/if - it is very 'bloated'. So code that is more verbose than necessary!
Modularity - having it in one single function - hard to understand...
This is a neat way of putting it! It is also known as 'design error' - anticipating and knowing what sort of error message you need to have. They have to be with enums.
Start Time - 17:17
Error Handling
Bob makes a good point - error handling is daunting at first as you feel as though you should be an expert at debugging to do it!
Example with roller coaster - basically using if/else statements to see what happens to someone's height if they can go on a roller coaster or not.
Problem with using else/if - it is very 'bloated'. So code that is more verbose than necessary!
Modularity - having it in one single function - hard to understand...
This is a neat way of putting it! It is also known as 'design error' - anticipating and knowing what sort of error message you need to have. They have to be with enums.
func checkHeightError(height: Int) {
if height > 200 {
throw ErrorMessage.maxHeight
} else if height < 140 {
throw ErrorMessage.minHeight
} else {
print("All is good!")
}
}
This is basically what Bob has done but it's not working.... Let's just check if this is an Xcode up to date thing....Not to worry Bob has the same issue too!
I've added in the keyword 'throws'. Also I need to use the 'do-try' block.
do {
try checkHeightError(height: 20)
} catch ErrorMessage.minHeight {
print("You are too short!")
} catch ErrorMessage.maxHeight {
print("You are too tall!")
}
So that clarifies some things - the do/try/catch block is SPECIFICALLY for error testing. It works this way because the function used 'throws' as the keyword in the initial line of it -
func checkHeightError(height: Int) throws {
Doing it with the error enum (that follows the error protocol), is safer and more maintainable.
class Course {
var name: String
init(name: String) {
if name == "" {
throw NameError.noName
} else {
self.name = name
print("You've created an object of Course!")
}
}
}
So there is a problem here - that the function needs the throws keyword....
init(name: String) throws {
That's the chap!
do {
let newCourse = try Course(name: "")
} catch NameError.noName {
print("You need to enter your name!")
}
Here is the do-try block for trying to see if an object is initialised with a string or no string. For the latter - the error message is used with the enum.
let newCourse = try? Course(name: "")
Right so in this case, the optional of ? is used, so nil is returned - which is safe!
let newCourse = try! Course(name: "")
Again, this is the force unwrap so should ONLY be used when we KNOW there is a value!
Finish Time - 17:53 (36 minutes)
Just one lesson today! But a 20 minute lesson took me 36 - the time to pause and check things etc. LOTS of useful stuff - I have so much more understanding of Error Handling! Looking forward to the next one, thanks Bob!
Comments
Post a Comment