当R中的CallingHandlers引发错误时,如何继续运行 [英] How to continue function when error is thrown in withCallingHandlers in R

查看:104
本文介绍了当R中的CallingHandlers引发错误时,如何继续运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个R函数编写一个测试用例,用于测试在函数的某一点是否正确抛出错误,并在发生错误时使测试继续进行,执行withCallingHandlers(...)。我使用这种方法:

  counter<  -  0 
withCallingHandlers({
testingFunction(df0 ,df1)
testingFunction(df2,df3)
testingFunction(df4,df5)

},warning = function(war){
print(paste消息))
},error = function(err){
print(paste(err $ message))
if(err $ message == paste(该函数应该抛出此错误消息,
在正确的时间。)){
计数器<< - counter + 1
}

})

stopifnot(counter == 2)

我遇到的问题是脚本是第一个错误之后退出(成功)被捕获,我不知道如何处理错误,以便在被捕获之后,CallingHandlers将继续执行下一部分。我明白它与重新启动对象有关,但我不知道如何正确使用它们。有没有人知道如何处理上述代码,以便即使发生错误也可以继续执行withCallingHandlers(...)?

解决方案

您可以通过调用 tryCatch 将每个调用包裹到 testingFunction 。:

 计数器<  -  0 
testForExpectedError< - function(expr){
tryCatch(expr,error = function {
print(paste(err $ message))
if(err $ message == paste(该函数应该抛出此错误消息,
在正确的时间。 {
计数器< - counter + 1
}
})
}

testForExpectedError(testingFunction(df0,df1))
testForExpectedError(testingFunction(df2,df3))
testForExpectedError(testingFunction(df4,df5))

stopifnot(counter == 2)
/ pre>

I'm writing a test case for an R function that tests whether an error is being thrown and caught correctly at a certain point in the function and I'm having some trouble getting the test to continue when an error is thrown during execution in withCallingHandlers(...). I'm using this approach:

counter <- 0
withCallingHandlers({
testingFunction(df0, df1)
testingFunction(df2, df3)
testingFunction(df4, df5)

}, warning=function(war){
    print(paste(war$message))
}, error=function(err){
    print(paste(err$message))
    if(err$message == paste("The function should throw this error message",
                              "at the right time.")){
        counter <<- counter + 1
    }

})

stopifnot(counter == 2)

The problem I'm running into is that the script is exiting after the first error is (successfully) caught and I'm not sure how to handle the error so that after it's caught, withCallingHandlers simply continues onto the next part of its execution. I understand that it has something to do with a restart object but I'm not sure how to use them correctly. Does anyone know how I could manipulate the above code so that execution of withCallingHandlers(...) continues even when an error is caught?

解决方案

You can just wrap each call to testingFunction with a call to tryCatch.:

counter <- 0
testForExpectedError <- function(expr) {
    tryCatch(expr, error=function(err) {
        print(paste(err$message))
        if(err$message == paste("The function should throw this error message",
                                "at the right time.")){
            counter <<- counter + 1
        }
    })
}

testForExpectedError(testingFunction(df0, df1))
testForExpectedError(testingFunction(df2, df3))
testForExpectedError(testingFunction(df4, df5))

stopifnot(counter == 2)

这篇关于当R中的CallingHandlers引发错误时,如何继续运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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