3.9 KiB
Why error handling is needed?
- I don't know.
- To control the execution flow.
- To make a program malware safe.
- Because, things can go wrong. CORRECT
1: Then, please watch the lecture again! :)
2: Actually yes, but that's not the main reason.
3: Come on!
What's a nil value?
- The dark matter that rules the universe.
- It's a zero value for pointers or pointer-based types. It means the value is uninitialized. CORRECT
- It's equal to empty string:
"" == nil
is true.
What's an error value?
- It stores the error details CORRECT
- A global variable which stores the error status.
- A global constant which stores the error status.
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.
How Go handles error handling?
- Using a throw/catch statement
- Using a simple if statement with nil comparison CORRECT
- Using a mechanism called tagging
1: There isn't a throw/catch statement in Go; unlike Java, C#, and so on... Go is explicit.
When you should handle the errors?
- After the main func ends.
- Before calling a function.
- Immediately, after calling a function which returns an error value. CORRECT
For which one of the following functions that you might want to handle the errors?
func Read() error
func Write() error
func String() string
func Reset()
- Read and Write CORRECT
- String and Reset
- Read, Write and Reset
- For neither of them
- For all of them
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.
Let's say a function returns a nil error value. So, what does that mean?
- The function call failed.
- The function call is successful. CORRECT
- The function call is in an indeterministic state. We can't know.
Let's say a function returns a non-nil error value. So, what does that mean?
- The function call failed. CORRECT
- The function call is successful.
- The function call is in an indeterministic state. We can't know.
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.
Does the following program correctly handles the 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(d)
}
}
- Yes. It prints the parsed duration if it's successful.
- No. It doesn't check for the errors.
- No. It prints the duration even when there's an error. CORRECT
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.
Does the following program correctly handles the 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)
}
- Yes. It prints the parsed duration if it's successful. CORRECT
- No. It doesn't check for the errors.
- No. It prints the duration even when there's an error.
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.