11-if/questions/4-error-handling.md
1: Then, please watch the lecture again! :)
2: Actually yes, but that's not the main reason.
3: Come on!
"" == nil is true.2, 3: There aren't any global variables in Go. There are only package level variables. And, since the error value is just a value, so it can be stored in any variable.
1: There isn't a throw/catch statement in Go; unlike Java, C#, and so on... Go is explicit.
func Read() error
func Write() error
func String() string
func Reset()
1: They return error values. So, you might want to handle the errors after you call them.
2: They don't return error values. So, you don't have to handle any errors.
3: Partially true. Try again.
1: Yep. Later on you'll learn that, this is not always true. Sometimes a function can return a non-nil error value, and the returned value may indicate something rather than an error. Search on Google: golang io EOF error if you're curious.
NOTE: This is what the ParseDuration function looks like:
func ParseDuration(s string) (Duration, error)
package main
import (
"fmt"
"time"
)
func main() {
d, err := time.ParseDuration("1h10s")
if err != nil {
fmt.Println(d)
}
}
1: Yes, it handles the error; however it does so incorrectly. Something is missing here. Look closely.
2: Actually, it does.
3: That's right. It shouldn't use the returned value when there's an error.
NOTE: This is what the ParseDuration function looks like:
func ParseDuration(s string) (Duration, error)
package main
import (
"fmt"
"time"
)
func main() {
d, err := time.ParseDuration("1h10s")
if err != nil {
fmt.Println("Parsing error:", err)
return
}
fmt.Println(d)
}
1: That's right. When there's an error, it prints a message and it quits from the program.
2: Actually, it does.
3: No, it does not. It only prints it when there isn't an error.