R tryCatch handling one kind of error

旧时模样 提交于 2019-12-24 15:24:09

问题


I wondering it is the way to check in tryCatch function kind of errors or warnings like in Java for example.

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;
        }

In R I only find solution for handling all error in one ways, example

result = tryCatch({
    expr
}, warning = function(w) {
    warning-handler-code
}, error = function(e) {
    error-handler-code
}, finally = {
    cleanup-code
}

回答1:


You could use try to handle errors:

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"


来源:https://stackoverflow.com/questions/47198200/r-trycatch-handling-one-kind-of-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!