0
0
R Programmingprogramming~10 mins

Error handling (tryCatch) in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error handling (tryCatch)
Start
Try block runs
Error?
NoTry block finishes normally
Yes
Catch error handler runs
Finally block runs (optional)
End
The program tries to run code in the try block. If an error happens, it jumps to the catch block. Finally block runs at the end no matter what.
Execution Sample
R Programming
result <- tryCatch({
  log(-1)
}, error = function(e) {
  "Error caught"
})
This code tries to compute log(-1), which causes an error, then catches it and returns "Error caught".
Execution Table
StepActionEvaluationResult
1Enter tryCatch, run try blocklog(-1)Error (domain error)
2Error detected in try blockError triggeredJump to error handler
3Run error handler functionfunction(e) returns "Error caught""Error caught"
4tryCatch returns error handler resultreturn value"Error caught"
💡 Error in log(-1) triggers error handler, which returns "Error caught" stopping error propagation.
Variable Tracker
VariableStartAfter try blockAfter error handlerFinal
resultundefinederror triggered"Error caught""Error caught"
Key Moments - 3 Insights
Why doesn't the program stop when log(-1) causes an error?
Because tryCatch catches the error at step 2 and runs the error handler instead of stopping the program.
What does the error handler function return?
It returns the string "Error caught" as shown in step 3 and 4 of the execution table.
What if there was no error handler provided?
The error would stop the program and show an error message instead of returning a value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after the error handler runs?
A"Error caught"
BNaN
Clog(-1)
DNULL
💡 Hint
Check the 'After error handler' and 'Final' columns in variable_tracker and step 3 in execution_table.
At which step does the program jump to the error handler?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Error detected in try block' row in execution_table.
If the try block code was log(10) instead of log(-1), what would happen?
AtryCatch returns NaN
BtryCatch returns log(10) value without error
CError handler runs and returns "Error caught"
DProgram stops with error
💡 Hint
Think about what happens when no error occurs in the try block (step 1 and 2).
Concept Snapshot
tryCatch({ try_code }, error = function(e) { handler })
- Runs try_code
- If error occurs, runs handler function
- Returns handler result or try_code result
- Prevents program crash on errors
- Useful for safe error handling
Full Transcript
This example shows how tryCatch in R handles errors. The code tries to compute log(-1), which causes an error. Instead of stopping, tryCatch catches the error and runs the error handler function, which returns the string "Error caught". This result is assigned to the variable result. The flow is: start tryCatch, run try block, detect error, jump to error handler, run handler, return handler result, end. Variables change from undefined to error triggered to "Error caught". This prevents the program from crashing and allows graceful error handling.