Reputation: 17418
I am trying this (apologies this is not reproducible but hopefully someone can help please):
for (row in 1:nrow(combinations)) {
tryCatch({
ba_search <- BayesianOptimization(
aq_function,
bounds = bayesian_search_bounds
# some more parameters
)
},
finally = {
if (exists("ba_search")) {
# do something with the results
} else {
# do some default because of exception
}
}
})
}
The intention is to execute:
# do some default because of exception
if an exception occurs, rather than terminate the script/loop.
Unfortunately, the script/loop still terminates. Any help to fix this would be very much appreciated. Thanks!
Upvotes: 2
Views: 42
Reputation: 41240
The error handling is done by an error
function, not by finally
, see Exceptions handling.
The following loop runs as expected:
for (row in 1:3) {
tryCatch({
print(row)
if (row==2) {ba_search <- "ba_search created"}
stop("Error triggered")
# some more parameters
},
error = function(c){
if (exists("ba_search")) {
# do something with the results
print(paste("Error after ba_search creation",c))
} else {
# do some default because of exception
print(paste("Error without ba_search:",c))
}
})
}
Upvotes: 1