Skip to content
Prev 244016 / 398506 Next

How to catch both warnings and errors?

Hmm... still not quite what I was hoping for... but thanks anyway. 

I would like to have the output in the following form:
$result
[1] 0

$warning
[1] NULL # or ""

$error
[1] NULL # or ""
$result
[1] NaN

$warning
[1] "Warning in log(-1) : NaNs produced" # or something similar

$error
[1] NULL # or ""
$result
[1] NULL # or NA 

$warning
[1] NULL # or ""

$error
[1] "Error in log("a") : Non-numeric argument to mathematical function"

Has anyone done that before? I think it's quite a natural problem although I could not find a solution online (dealing with *both* warnings *and* errors). 

Cheers,

Marius