Why error handling is required
📖 Scenario: Imagine you are writing a program that reads numbers from a list and divides 100 by each number. Sometimes, the number might be zero, which causes a problem called a "division by zero" error. To keep the program running smoothly, you need to handle such errors.
🎯 Goal: Build a simple Go program that divides 100 by numbers in a list and handles the error when division by zero happens.
📋 What You'll Learn
Create a slice of integers called
numbers with the values 10, 5, 0, 2Create a variable called
result to store the division resultUse a
for loop with variable num to iterate over numbersInside the loop, check if
num is zero and handle the error by printing "Cannot divide by zero"If no error, calculate
result = 100 / num and print the resultThe program should continue running even if an error occurs
💡 Why This Matters
🌍 Real World
Programs often get unexpected inputs or face problems like missing files or network issues. Error handling helps manage these situations gracefully.
💼 Career
Understanding error handling is essential for writing reliable software, a key skill for any software developer or engineer.
Progress0 / 4 steps