R中无法获得“try-error”类

时间:2016-10-27 09:00:20

标签: r error-handling

我需要检查我与数据库的连接,是否已过期: 当我尝试执行查询时,我得到了这样的错误

> dbGetQuery(con,"select * from users limit 1")
Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not run statement: could not receive data from server: Software caused connection abort (0x00002745/10053)
)
NULL
Warning message:
In postgresqlQuickSQL(conn, statement, ...) :
  Could not create executeselect * from users limit 1

好的,我需要类似“如果错误然后打印(”连接已过期“)”。 我试图解决这个问题:

> class(try(dbGetQuery(con,"select * from users limit 1"), silent = T))
Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not run statement: could not receive data from server: Software caused connection abort (0x00002745/10053)
)
[1] "NULL"
Warning message:
In postgresqlQuickSQL(conn, statement, ...) :
  Could not create executeselect * from users limit 1

再次收到此错误并发出警告。尝试表达式的类是“NULL”,而不是“try-error”。 但是当我这样做时:

> class(try(log("hi"), silent = T))
[1] "try-error"

我没有错误和尝试错误类

为什么?

UPD。 通过

解决了这个问题
>options(warn = 2, show.error.messages = F)

然后

class(try(dbGetQuery(con,"select * from users limit 1"), silent = T))
[1] "try-error"

0 个答案:

没有答案