更改错误配置后脚本不会停止错误

时间:2018-06-17 13:18:42

标签: r error-handling rstudio

我想更改R上的错误配置以修改错误消息;问题是,在我更改配置后,我的脚本在找到错误后不会停止。

我使用以下函数更改它:

options(error = function() {
  cat("The error will be print here:\n", geterrmessage());
  traceback(3);
  stop(geterrmessage())
})

如果我在R或Rstudio中工作,这个功能很有效但是当我使用 Rscript 从终端运行一些代码时,代码将在发生错误后继续运行。例如,如果您复制并保存下面的脚本并从终端运行它,您将看到脚本在停止功能之后或 non_existent_variable <生成错误之后没有停止/ em>的

  #Testing logging and stop
  #!/usr/bin/env Rscript
  args <- commandArgs(trailingOnly = TRUE)

  #Changeing the configuration of the error handler (Not to run localy)
  options(error = function() {
        cat("The error will be print here:\n", geterrmessage());
        traceback(3);
        stop(geterrmessage())})

  stop('Testing stop function')
  a <- non_existent_variable #This will generate an error

  print("The script didn't stop")
  print(args)
  cat("The script didn't stop")

(我不想使用tryCatch()或类似函数)

由于

0 个答案:

没有答案