我想知道这是检查tryCatch函数类错误或警告的方法,例如在Java中。
try {
driver.findElement(By.xpath(locator)).click();
result= true;
} catch (Exception e) {
if(e.getMessage().contains("is not clickable at point")) {
System.out.println(driver.findElement(By.xpath(locator)).getAttribute("name")+" are not clicable");
} else {
System.err.println(e.getMessage());
}
} finally {
break;
}
在R中我只找到以一种方式处理所有错误的解决方案,例如
result = tryCatch({
expr
}, warning = function(w) {
warning-handler-code
}, error = function(e) {
error-handler-code
}, finally = {
cleanup-code
}
答案 0 :(得分:1)
您可以使用try
来处理错误:
result <- try(log("a"))
if(class(result) == "try-error"){
error_type <- attr(result,"condition")
print(class(error_type))
print(error_type$message)
if(error_type$message == "non-numeric argument to mathematical function"){
print("Do stuff")
}else{
print("Do other stuff")
}
}
# [1] "simpleError" "error" "condition"
# [1] "non-numeric argument to mathematical function"
# [1] "Do stuff"
答案 1 :(得分:0)
我们还可以使用 tryCatch 处理错误并分析发出的消息,在您的示例中,它将是 library(tidyverse)
original <- tibble(x = c(1,1,1,2,2,2,4,4,4))
aggregated <- original %>% count(x)
deaggregated <- aggregated %>% uncount(weights = n)
。我已将您的示例改编为适用于这种情况。
e$message
(我不确定 e$message 是否可以有多个字符串,在这种情况下,您可能还需要考虑使用 result = tryCatch({
expr
}, warning = function(w) {
warning-handler-code
}, error = function(e) {
if(e$message == "This error should be treated in some way"){
error-handler-code-for-one-type-of-error-message
}
else{
error-handler-code-for-other-errors
}
}, finally = {
cleanup-code
}
)
函数 any