[Rd] capturing multiple warnings in tryCatch()

Fox, John j|ox @end|ng |rom mcm@@ter@c@
Thu Dec 2 22:02:25 CET 2021

Dear R-devel list members,

Is it possible to capture more than one warning message using tryCatch()? The answer may be in ?conditions, but, if it is, I can't locate it.

For example, in the following only the first warning message is captured and reported:

> foo <- function(){
+   warning("warning 1")
+   warning("warning 2")
+ }

> foo()
Warning messages:
1: In foo() : warning 1
2: In foo() : warning 2

> bar <- function(){
+   tryCatch(foo(), warning=function(w) print(w))
+ }

> bar()
<simpleWarning in foo(): warning 1>

Is there a way to capture "warning 2" as well?

Any help would be appreciated.


John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
Web: http://socserv.mcmaster.ca/jfox/

More information about the R-devel mailing list