Challenge - 5 Problems
tryCatch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this tryCatch example?
Consider the following R code using tryCatch. What will be printed when it runs?
R Programming
result <- tryCatch({
stop("Error happened")
10
}, error = function(e) {
"Caught error"
})
print(result)Attempts:
2 left
💡 Hint
Remember that stop() causes an error and tryCatch handles it.
✗ Incorrect
The stop() function triggers an error, so the error handler runs and returns "Caught error". The print shows this string.
❓ Predict Output
intermediate2:00remaining
What does this tryCatch return when no error occurs?
Look at this R code. What is the value of 'result' after running?
R Programming
result <- tryCatch({
5 + 3
}, error = function(e) {
0
})
print(result)Attempts:
2 left
💡 Hint
No error is triggered in the expression 5 + 3.
✗ Incorrect
Since 5 + 3 runs without error, tryCatch returns 8 directly.
❓ Predict Output
advanced2:00remaining
What error does this code raise?
What error message will this R code produce?
R Programming
tryCatch({
log("a")
}, error = function(e) {
stop("Custom error")
})Attempts:
2 left
💡 Hint
The error handler re-throws a new error with stop().
✗ Incorrect
The original error is caught, but the handler calls stop() with "Custom error", so that error is raised.
🧠 Conceptual
advanced2:00remaining
Which option correctly catches warnings with tryCatch?
You want to catch warnings in R using tryCatch. Which code snippet correctly catches a warning?
Attempts:
2 left
💡 Hint
Use the 'warning' argument to catch warnings.
✗ Incorrect
The warning argument in tryCatch handles warnings. Others like error or message do not catch warnings.
🔧 Debug
expert2:00remaining
Why does this tryCatch not catch the error?
This R code does not catch the error as expected. What is the reason?
R Programming
tryCatch({
stop("Fail")
}, warning = function(w) {
print("Warning caught")
})Attempts:
2 left
💡 Hint
stop() triggers an error, not a warning.
✗ Incorrect
tryCatch only catches errors if an error handler is provided. Here only warning handler is given, so error is not caught and stops execution.