📖 Scenario: Imagine you are writing a small R program that divides numbers. Sometimes, the divisor might be zero, which causes an error. You want to handle this error gracefully so your program doesn't stop unexpectedly.
🎯 Goal: You will create a program that divides two numbers and uses tryCatch to handle division by zero errors by showing a friendly message instead of stopping.
📋 What You'll Learn
Create two numeric variables named numerator and denominator with exact values
Create a variable named result to store the division result
Use tryCatch to catch division errors
Print the result or an error message
💡 Why This Matters
🌍 Real World
Handling errors like division by zero is important in data analysis and scientific computing to avoid program crashes.
💼 Career
Error handling is a key skill for data scientists and programmers to write robust and user-friendly code.
Progress0 / 4 steps
1
Create numerator and denominator variables
Create a numeric variable called numerator and set it to 10. Create another numeric variable called denominator and set it to 0.
R Programming
Hint
Use the assignment operator <- to create variables.
2
Create a result variable with default value
Create a variable called result and set it to NA to hold the division result before calculation.
R Programming
Hint
Use NA to represent a missing value in R.
3
Use tryCatch to perform division safely
Use tryCatch to divide numerator by denominator and assign the result to result. If an error occurs, assign the string "Error: Division by zero" to result.
R Programming
Hint
Inside tryCatch, put the division expression in the first argument, and define an error function that returns the error message string.
4
Print the result
Write a print statement to display the value of result.
R Programming
Hint
Use print(result) to show the output.
Practice
(1/5)
1. What is the main purpose of using tryCatch in R?
easy
A. To print messages to the console
B. To speed up the execution of loops
C. To handle errors and warnings without stopping the program
D. To create new variables automatically
Solution
Step 1: Understand the role of tryCatch
tryCatch is designed to catch errors and warnings during code execution.
Step 2: Identify the effect on program flow
It prevents the program from stopping abruptly by handling these issues gracefully.
Final Answer:
To handle errors and warnings without stopping the program -> Option C
Quick Check:
Error handling = To handle errors and warnings without stopping the program [OK]
Hint: tryCatch catches errors so program keeps running [OK]
Common Mistakes:
Thinking tryCatch speeds up code
Confusing tryCatch with printing functions
Assuming tryCatch creates variables
2. Which of the following is the correct syntax to catch an error using tryCatch in R?
easy
A. tryCatch(expr, except = function(e) { print('Error caught') })
B. tryCatch(expr, catch = function(e) { print('Error caught') })
C. tryCatch(expr, onError = function(e) { print('Error caught') })
D. tryCatch(expr, error = function(e) { print('Error caught') })
Solution
Step 1: Recall tryCatch argument names
The correct argument to catch errors is error, not catch, onError, or except.
Step 2: Match syntax with correct argument
tryCatch(expr, error = function(e) { print('Error caught') }) uses error = function(e) { ... } which is the proper syntax.