0
0
Goprogramming~10 mins

Handling errors in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Handling errors
Start function
Try operation
Error returned?
NoUse result
Yes
Handle error
End function
The program tries an operation, checks if an error occurred, handles it if yes, otherwise uses the result.
Execution Sample
Go
package main
import (
 "errors"
 "fmt"
)

func divide(a, b int) (int, error) {
 if b == 0 {
  return 0, errors.New("cannot divide by zero")
 }
 return a / b, nil
}

func main() {
 result, err := divide(10, 0)
 if err != nil {
  fmt.Println("Error:", err)
 } else {
  fmt.Println("Result:", result)
 }
}
This code tries to divide 10 by 0, checks for error, and prints the error message.
Execution Table
StepActionConditionResultOutput
1Call divide(10, 0)b == 0?TrueReturn error "cannot divide by zero"
2In main, check err != nilerr != nil?TruePrint "Error: cannot divide by zero"
3End program---
💡 Error detected in divide, handled in main, program ends after printing error.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
a10101010
b0000
resultundefined000
errundefinederror("cannot divide by zero")error("cannot divide by zero")error("cannot divide by zero")
Key Moments - 2 Insights
Why do we check if err != nil after calling divide?
Because Go functions return errors as values, we must check if err is not nil to know if an error happened, as shown in execution_table step 2.
What happens if we try to use result without checking err?
If err is not nil, result may be invalid or zero. Checking err prevents using wrong data, as seen in step 2 where error is handled instead of using result.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of err after step 1?
Aerror("cannot divide by zero")
Bnil
C0
Dundefined
💡 Hint
Check the 'Result' column in step 1 of the execution_table.
At which step does the program print the error message?
AStep 1
BStep 2
CStep 3
DNo print occurs
💡 Hint
Look at the 'Output' column in the execution_table for step 2.
If divide was called with b=2 instead of 0, what would happen in step 1?
AReturn result 0 and error nil
BReturn error "cannot divide by zero"
CReturn result 5 and error nil
DPanic and stop program
💡 Hint
Recall that divide returns error only if b == 0, otherwise returns a/b and nil error.
Concept Snapshot
Go error handling:
- Functions return (value, error)
- Check if error != nil after call
- Handle error before using value
- Use errors.New() to create errors
- Prevent program crashes by checking errors
Full Transcript
This example shows how Go handles errors by returning an error value from functions. The divide function returns an error if dividing by zero. The main function checks if the error is not nil before using the result. If there is an error, it prints the error message. This prevents using invalid data and keeps the program safe. The execution table traces each step: calling divide, checking error, printing error, and ending. Variables track the values of inputs, result, and error through the steps. Key moments explain why checking error is important and what happens if skipped. The quiz tests understanding of error values and program flow. This method is simple and clear for beginners to handle errors safely in Go.