Cause of Error
Syntax Error: The code doesn’t follow the right structure. For instance, if we write print(“Hello, world!”) but forget to close the quotation marks, we’ll see a syntax error.
# Incorrect syntax: missing closing quotation mark
print("Hello, world!)
Output:
Error: unexpected symbol in:
"# Incorrect syntax: missing closing quotation mark
print("Hello"
Runtime Error: When the code is written correctly, but it tries to do something impossible. For example, if we attempt to divide a number by zero, we’ll encounter a runtime error because we can’t divide by zero.
# Attempting to divide by zero
result <- 10 / 0
result
Output:
[1] Inf
Here, R returns Inf, which stands for “infinity,” because dividing any non-zero number by zero results in infinity in R.
Logical Error: It’s happen when the code looks right and makes sense, but it doesn’t give the expected results. For example, try to add two words together using the addition operator (+), it won’t work as expected because we can’t add words together like numbers.
# Adding two words together
result <- "Hello" + "World"
Output:
Error in "Hello" + "World" : non-numeric argument to binary operator
How to Implement Error Handling in R Programming
Error handling is a crucial aspect of programming that allows to identify, and gracefully manage errors or exceptions that may occur during the execution of code. In R Programming Language, effective error handling can enhance the reliability of scripts and applications.
Contact Us