syntax
# result = tryCatch({
# expr
# }, warning = function(warning-condition) {
# warning-handler-code
# }, error = function(error-condition) {
# error-handler-code
# }, finally={
# cleanup-code
# })
example1
inputs = list(1, 2, 4, -5, 'oops', 0, 10)
log(1)
## [1] 0
log(2)
## [1] 0.6931472
log(4)
## [1] 1.386294
log(-5)
## Warning in log(-5): NaNs produced
## [1] NaN
#log("oops")
log(0)
## [1] -Inf
log(10)
## [1] 2.302585
log(-(-5))
## [1] 1.609438
Normal for loop
# for(input in inputs) {
# print(paste("log of", input, "=", log(input)))
# }
#log(-5) #NAN
#log("oops") #error
#its stop then the error occurs
try block
for(input in inputs) {
try(print(paste("log of", input, "=", log(input))))
}
## [1] "log of 1 = 0"
## [1] "log of 2 = 0.693147180559945"
## [1] "log of 4 = 1.38629436111989"
## Warning in log(input): NaNs produced
## [1] "log of -5 = NaN"
## [1] "log of 0 = -Inf"
## [1] "log of 10 = 2.30258509299405"
#This skips over the error-causing non-numeric input with an error message
#(you can suppress the error message with the silent=T argument to try),
#and continues on with the rest of the input.
for(input in inputs) {
try(print(paste("log of", input, "=", log(input))),silent = T)
}#removes error
## [1] "log of 1 = 0"
## [1] "log of 2 = 0.693147180559945"
## [1] "log of 4 = 1.38629436111989"
## Warning in log(input): NaNs produced
## [1] "log of -5 = NaN"
## [1] "log of 0 = -Inf"
## [1] "log of 10 = 2.30258509299405"
trycatch block
1
for(input in inputs) {
tryCatch(print(paste("log of", input, "=", log(input))),
warning = function(w) {print(paste("negative argument", input))},
error = function(e) {print(paste("non-numeric argument", input))}
)}
## [1] "log of 1 = 0"
## [1] "log of 2 = 0.693147180559945"
## [1] "log of 4 = 1.38629436111989"
## [1] "negative argument -5"
## [1] "non-numeric argument oops"
## [1] "log of 0 = -Inf"
## [1] "log of 10 = 2.30258509299405"
2
robustLog = function(x) {
tryCatch(log(x),
warning = function(w) {print(paste("negative argument", x));
log(-x)},
error = function(e) {print(paste("non-numeric argument", x));
NaN})
}
for(input in inputs) {
print(paste("robust log of", input, "=", robustLog(input)))
}
## [1] "robust log of 1 = 0"
## [1] "robust log of 2 = 0.693147180559945"
## [1] "robust log of 4 = 1.38629436111989"
## [1] "negative argument -5"
## [1] "robust log of -5 = 1.6094379124341"
## [1] "non-numeric argument oops"
## [1] "robust log of oops = NaN"
## [1] "robust log of 0 = -Inf"
## [1] "robust log of 10 = 2.30258509299405"
example
a<-list(1,2,3,4,-4,5,0,10,"sujith")
#sqrt(a)
a_sqrt<-list()
we <- function(x) {
tryCatch(sqrt(x),
warning = function(w){ print("warning");
sqrt(-x)},
error = function(e){print("error");
NaN })
}
for(i in a){
print(paste("squareroot of ",i,"=",we(i)))
a_sqrt<-append(a_sqrt,we(i))
}
## [1] "squareroot of 1 = 1"
## [1] "squareroot of 2 = 1.4142135623731"
## [1] "squareroot of 3 = 1.73205080756888"
## [1] "squareroot of 4 = 2"
## [1] "warning"
## [1] "squareroot of -4 = 2"
## [1] "warning"
## [1] "squareroot of 5 = 2.23606797749979"
## [1] "squareroot of 0 = 0"
## [1] "squareroot of 10 = 3.16227766016838"
## [1] "error"
## [1] "squareroot of sujith = NaN"
## [1] "error"
a_sqrt
## [[1]]
## [1] 1
##
## [[2]]
## [1] 1.414214
##
## [[3]]
## [1] 1.732051
##
## [[4]]
## [1] 2
##
## [[5]]
## [1] 2
##
## [[6]]
## [1] 2.236068
##
## [[7]]
## [1] 0
##
## [[8]]
## [1] 3.162278
##
## [[9]]
## [1] NaN