R tryCatch处理一种错误 [英] R tryCatch handling one kind of error

查看:121
本文介绍了R tryCatch处理一种错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是检查tryCatch函数类型的错误或警告的方法,例如在Java中.

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

在R中,我只找到一种方法来处理所有错误的解决方案,例如

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
}

推荐答案

您可以使用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处理一种错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆