gpt4 book ai didi

R tryCatch 处理一种错误

转载 作者:行者123 更新时间:2023-12-03 07:41:53 26 4
gpt4 key购买 nike

我想知道这是检查 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
}

最佳答案

您可以使用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"

关于R tryCatch 处理一种错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47198200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com