1

I'd like to catch an invalid input error within a timeout error. I wrote the script as follows. However, the timeout didn't work in this case and the program run forever. Could anyone please have a check and let me know how to fix it? Thank you.

for (lb in 1:100) {
  rs <- tryCatch(
          expr = {
            cat("helloworld", "\n");
            withTimeout({
              rs_son = tryCatch(
                expr = {
                  flag = FALSE
                  cat("start solnl", "\n");
                  ret = solnl(X, objfun = obj, confun = con, lb=lb, ub=ub);
                  cat("finish solnl", "\n");
                },
                error = function(e) {flag <- TRUE}
              )
              if(flag) {next}
            },
            timeout = 10)
          },
          TimeoutException = function(ex) {
            cat("Timeout. Skipping.\n")
          }
        )
}

To be more clear, this is what I'd like to achieve. The for loop should continue no matter whether this is an invalid input error for the solnl function or timeout error.

S Z
  • 31
  • 4

1 Answers1

1

There are multiple issues here. First, there is no loop to call next, second, flag is only defined inside the error handling function, not outside of tryCatch(), third, the outer tryCatch() has no impact here.

I added some further cat messages that hopefully shed some light on what is going on

edit: updated the answer with proper for loop

set.seed(0)
rs <- character()
for (lb in 1:10) {
  
  rs[lb] <- R.utils::withTimeout({
    rs_son = tryCatch(
      expr = {
        flag <- FALSE # has no impact
        Sys.sleep(sample(0:2, size = 1))
        cat("start solnl", "\n")
        ret = solnl(X, objfun = obj, confun = con, lb=lb, ub=ub)
        cat("finish solnl", "\n")
      },
      error = function(e) {
        cat(as.character(e))
        flag <- TRUE # equals return(TRUE)
      }
    )
    if(rs_son) {
      return("NEXT OUT")
    }
    return("OUT")
  },
  timeout = 1)
  
  
}
#> Error in Sys.sleep(sample(0:2, size = 1)): reached elapsed time limit
#> start solnl 
#> Error in solnl(X, objfun = obj, confun = con, lb = lb, ub = ub): could not find function "solnl"
#> Error in Sys.sleep(sample(0:2, size = 1)): reached elapsed time limit
#> start solnl 
#> Error in solnl(X, objfun = obj, confun = con, lb = lb, ub = ub): could not find function "solnl"
#> Error in Sys.sleep(sample(0:2, size = 1)): reached elapsed time limit
#> start solnl 
#> Error in solnl(X, objfun = obj, confun = con, lb = lb, ub = ub): could not find function "solnl"
#> Error in Sys.sleep(sample(0:2, size = 1)): reached elapsed time limit
#> Error in Sys.sleep(sample(0:2, size = 1)): reached elapsed time limit
#> start solnl 
#> Error in solnl(X, objfun = obj, confun = con, lb = lb, ub = ub): could not find function "solnl"
#> start solnl 
#> Error in solnl(X, objfun = obj, confun = con, lb = lb, ub = ub): could not find function "solnl"

rs
#>  [1] "NEXT OUT" "NEXT OUT" "NEXT OUT" "NEXT OUT" "NEXT OUT" "NEXT OUT"
#>  [7] "NEXT OUT" "NEXT OUT" "NEXT OUT" "NEXT OUT"
mnist
  • 6,571
  • 1
  • 18
  • 41
  • Thanks a lot for your reply. It was my bad that I didn't paste the for loop at the beginning (I've updated the question). Since there is a for loop, I couldn't use return command. Could you please shed more insights how to fix? My goal here is to make the outer trycatch() work. – S Z Mar 14 '21 at 23:26
  • To be more clear, this is what I'd like to achieve. The for loop should continue no matter whether this is an invalid input error for the solnl function or timeout error. – S Z Mar 14 '21 at 23:35
  • I updated my answer and still do not see the necessity of the outer tryCatch. As you can see, the error varies whether it is caused by a timeout or because the function is not available. The loop works and the result stays the same – mnist Mar 15 '21 at 16:19
  • Did you have a chance to check my latest answer? Please mark it as accepted if it satisfies your needs or leave a comment otherwise – mnist Mar 25 '21 at 07:11
  • Sorry for my late. But it didn't work. The outer loop timeout doesn't work in my case and the solnl gets stuck in the middle. :( – S Z Mar 28 '21 at 16:12
  • When the solnl got stuck, I have to force quit the program. I guess the reason could be solnl. https://stackoverflow.com/questions/43148981/r-not-responding-request-to-interrupt-stop-process – S Z Mar 28 '21 at 16:40
  • well, tryCatch needs an actual error to detect an error. I would suggest to carefully examine when(!) and then why soln does not work and try to fix the issue there – mnist Mar 28 '21 at 18:49
  • As mentioned by that link, there is almost no way to fix since solnl calls some external resources (maybe c++). So withtimeout doesn't work. – S Z Apr 03 '21 at 18:54