r - Try and catch hierarchy 在 R 中是如何工作的?

标签 r try-catch

我想知道 try 和 catch 层次结构在 R 中是如何工作的。

我认为如果我有一个包含 try 和 catch 的函数,如果出现错误,它就会被捕获并且代码可以继续运行。 如果您有第二个带有 try 和 catch 的函数,在某些时候需要第一个函数,我认为如果第一个函数发现错误,将返回错误值,因此第二个函数将考虑错误值工作,并且它将不要在 catch 语句中输入。

然而,这不是这样工作的,它还进入了第二个函数错误捕获语句。 我将尝试简化并举一个例子(尽管这些功能没有意义)

第一个函数 readUrl 提取 url 的 html 输出(如果它存在),如果不存在则返回 NA。 第二个函数执行 readUrl 函数和求和,并返回列表中的两个元素。

library(dplyr)

#Function 1

readUrl <- function(url){
  out <- tryCatch({
    message(paste0("Trying to enter the url: ", url))
    url <- rvest::read_html(url)
  },
  error = function(cond){
    message(paste("URL does not seem to exist: ", url))
    message("It has entered in the first function catch")
    message(cond)
    # Return value in case of error
    return(NA)
  },
  warning = function(cond){
    message(paste("URL caused a warning:", url))
    message("Here's the original warning message:")
    message(cond)
    return(NULL)
  }
  )
  return (out)
}


#function 2

func2 <- function (variant.id){
  
  output <- tryCatch({
    url1 <- paste0("https://www.ncbi.nlm.nih.gov/clinvar/variation/", variant.id)
    url.extract <- readUrl(url1)
    sum <- 2+2
    list(url = url.extract, sum = sum)
  },
  error = function(cond){
    message("It has entered in the second function Catch ")
    # Return value in case of error
    return(cond)
  },
  warning = function(cond){
    return(NULL)
  }
  )
  return (output)
}


#We enter a valid id It works fine
func2("579822")

#We enter an invalid id , it enters both try and Catch instead of only the first one
func2("579822G")

我期望 func2("579822G") 输出是这样的列表:

$url
NA
$sum
[1] 4

我该如何处理这些? 我知道这两个功能没有意义,但我确实需要按照这个逻辑做一些事情。 提前致谢。

最佳答案

readUrl 的错误处理程序中的

message(cond) 是导致不稳定行为的原因。如果您想在返回调用例程之前回显异常的详细信息,请将其替换为 print(cond)。此版本按照您期望的方式工作:

readUrl <- function(url){
  out <- tryCatch({
    message(paste0("Trying to enter the url: ", url))
    url <- rvest::read_html(url)
  },
  error = function(cond){
    message(paste("URL does not seem to exist: ", url))
    message("It has entered in the first function catch")
    print(cond)
    # Return value in case of error
    return(NA)
  },
  warning = function(cond){
    message(paste("URL caused a warning:", url))
    message("Here's the original warning message:")
    print(cond)
    return(NULL)
  }
  )
  return (out)
}


#function 2

func2 <- function (variant.id){
  
  output <- tryCatch({
    url1 <- paste0("https://www.ncbi.nlm.nih.gov/clinvar/variation/", variant.id)
    url.extract <- readUrl(url1)
    sum <- 2+2
    list(url = url.extract, sum = sum)
  },
  error = function(cond){
    message("It has entered in the second function Catch ")
    # Return value in case of error
    return(cond)
  },
  warning = function(cond){
    return(NULL)
  }
  )
  return (output)
}


#We enter a valid id It works fine
func2("579822")

#We enter an invalid id , it enters both try and Catch instead of only the first one
func2("579822G")

关于r - Try and catch hierarchy 在 R 中是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74055778/

相关文章:

Java HttpURLConnection 神秘死亡

Java - 在 Double.parseDouble() 的 'catch' block 中包含负值

.net - 简化字符串异常检查

R - 回溯期的总和范围,回溯期的除总和 - 优于 R

php - 自定义错误处理程序和尝试 - 捕获 - 如何干净地处理错误和警告

r - 使用 ggplot2 在条形图中放置误差线

r - 首先计算行号,然后按条件插入新行

javascript - 设置状态 : can only update a mounted or mounting component react native/undefined is not an object

r - 如何将图例添加到 R 中的拟合优度图?

r - 如何使用 R 中不同数据集创建的模型来预测新数据集的结果?