Custom Errors in R Programming Language
# A custom error type usually has the suffix "Error".
argError <- function(arg, message) {
structure(list(arg = arg, message = message), class = "argError")
}
# Adding this Error method makes argError implement
# the error interface.
print.argError <- function(x, ...) {
cat(sprintf("%d - %s", x$arg, x$message))
}
f <- function(arg) {
if (arg == 42) {
# Return our custom error.
return(list(value = -1, error = argError(arg, "can't work with it")))
}
list(value = arg + 3, error = NULL)
}
main <- function() {
# tryCatch is R's equivalent to try-catch in other languages
result <- tryCatch({
f(42)
}, error = function(e) {
return(list(value = NULL, error = e))
})
if (!is.null(result$error) && inherits(result$error, "argError")) {
cat(result$error$arg, "\n")
cat(result$error$message, "\n")
} else {
cat("err doesn't match argError\n")
}
}
main()In R, we don’t have a built-in error system quite like Go’s, but we can achieve similar functionality using custom S3 classes and the tryCatch function.
We define
argErroras a function that creates a list withargandmessagefields, and assigns it the class “argError”.We define a custom print method for the
argErrorclass, which mimics theError()method in the Go example.The
ffunction is similar to the Go version, but it returns a list withvalueanderrorfields instead of using multiple return values.In the
mainfunction, we usetryCatchto handle potential errors. This is similar to Go’s error handling, but uses R’s exception mechanism.We check if the error is of class “argError” using
inherits(), which is similar to theerrors.As()function in Go.
When you run this script, it will output:
42
can't work with itThis R code demonstrates how to create and use custom error types, mirroring the functionality of the original Go example while using R’s idiomatic approaches to error handling and object-oriented programming.