Catching runtime errors
π Scenario: Imagine you are building a simple calculator app that divides two numbers. Sometimes users might enter zero as the divisor, which causes an error. You want to catch this error and show a friendly message instead of crashing.
π― Goal: You will create a program that safely divides two numbers and catches any runtime errors using try and catch. If an error happens, it will show a clear message.
π What You'll Learn
Create two variables called
numerator and denominator with exact valuesCreate a variable called
result and set it to nullUse a
try block to divide numerator by denominator and assign to resultUse a
catch block to set result to the string 'Error: Cannot divide by zero'Print the value of
resultπ‘ Why This Matters
π Real World
Handling errors prevents apps from crashing and helps show friendly messages to users.
πΌ Career
Error handling is a key skill for developers to build reliable and user-friendly software.
Progress0 / 4 steps