Reputation: 253
II don't understand how to catch an error.
For example i wrote a script:
morph_input = tryCatch(prepareMorphObjectFromFiles(InputConfig,InputGOI),
error=stop("Please Start Over,your Data Sets or uploaded GOI are not suitable."))
I want that if the function prepareMorphObjectFromFiles(InputConfig,InputGOI)
will break, just then the error will pop out. but it always pop out even if the function didn't break.
Upvotes: 1
Views: 158
Reputation: 109864
@gagolews gives what most would prefer. I like using just try
and it helps me to think more linear about my code (i.e. in steps).
out <- try({
if (runif(1) > .7) stop("catch me if you can!")
else "OK"
}, silent = TRUE)
if(inherits(out, "try-error")) message("went bad")
Upvotes: 2
Reputation: 13046
You should pass an error handling function as the error
argument in tryCatch()
. Otherwise it indeed will always be evaluated. An example:
tryCatch({
if (runif(1) > 0.8) stop("catch me if you can!")
else "OK"
},
error=function(err) {
# an error handler
cat("An error occured.\n")
})
Upvotes: 4