Reputation: 1190
I am attempting to get my head around error handling in R. From reading the docs, I cannot find a clear definition of the correct syntax.
Here is a self contained example:
foo <- function(dat) {
print('Calling foo()')
print(dat[,'r1'])
print(dat[,'r2'])
print(dat[,'r3'])
print('Finished foo()')
return(dat$r3)
}
bar <- function(dat) {
print('Calling bar()')
print(dat[,'r1'])
print(dat[,'r2'])
print('Finished bar()')
return(dat$r2)
}
d1 <- data.frame(r1 = 1:10, r2 = 11:20, r3 = 21:30)
d2 <- d1[c("r1", "r2")]
print('Starting')
print('trying d1')
myVal1 <- tryCatch(foo(d1), error = bar(d1))
print('finished d1')
print('trying d2')
myVal2 <- tryCatch(foo(d2), error = bar(d2))
print('finished d2')
print('Done')
Expected output:
[1] "Starting"
[1] "trying d1"
[1] "Calling foo()"
[1] 1 2 3 4 5 6 7 8 9 10
[1] 11 12 13 14 15 16 17 18 19 20
[1] 21 22 23 24 25 26 27 28 29 30
[1] "Finished foo()"
[1] "finished d1"
[1] "trying d2"
[1] "Calling bar()"
[1] 1 2 3 4 5 6 7 8 9 10
[1] 11 12 13 14 15 16 17 18 19 20
[1] "Finished bar()"
[1] "finished d2"
[1] "Done"
Actual output:
[1] "Starting"
[1] "trying d1"
[1] "Calling bar()"
[1] 1 2 3 4 5 6 7 8 9 10
[1] 11 12 13 14 15 16 17 18 19 20
[1] "Finished bar()"
[1] "Calling foo()"
[1] 1 2 3 4 5 6 7 8 9 10
[1] 11 12 13 14 15 16 17 18 19 20
[1] 21 22 23 24 25 26 27 28 29 30
[1] "Finished foo()"
[1] "finished d1"
[1] "trying d2"
[1] "Calling bar()"
[1] 1 2 3 4 5 6 7 8 9 10
[1] 11 12 13 14 15 16 17 18 19 20
[1] "Finished bar()"
[1] "Calling foo()"
[1] 1 2 3 4 5 6 7 8 9 10
[1] 11 12 13 14 15 16 17 18 19 20
Error in tryCatchOne(expr, names, parentenv, handlers[[1L]]) :
attempt to apply non-function
My questions:
Why does this tryCatch()
call end up running both functions?
Why do I get the error reported above?
Is my syntax of tryCatch(someFunction(), error = someOtherFunction())
incorrect?
Thanks!
Upvotes: 1
Views: 5678
Reputation: 42649
error
wants a function. You have passed an expression that is a result of evaluating a function on an argument. Also, error
is not passing the data frame to bar
. The error text is passed.
Replacing this:
myVal2 <- tryCatch(foo(d2), error = bar(d2))
with this:
myVal2 <- tryCatch(foo(d2), error = function(e) { cat('In error handler\n'); print(e); e })
results in this behavior:
[1] "Calling foo()"
[1] 1 2 3 4 5 6 7 8 9 10
[1] 11 12 13 14 15 16 17 18 19 20
In error handler
<simpleError in `[.data.frame`(dat, , "r3"): undefined columns selected>
Note that the handler is called. And myVal2
is set to the result of the error handler:
> myVal2
<simpleError in `[.data.frame`(dat, , "r3"): undefined columns selected>
Upvotes: 5