Greetings,
 
Error handling does not seem to work as expected.
When a handler for general conditions is provided in first position in the 
handler list it will be called 
regardless of what condition has been signalled.
 
See sample code, move  "condition = function(c){...}" to first position in 
handler list.
This indicates that the determination of which condition handler is appropriate 
does not work like
virtual function dispatch.
 
I also would like to know what happens if some handlers are not provided.
For example if a warning handler is not provided and a warning is signalled 
does this invoke a
default handler and how does this handler know which value to return from the 
tryCatch block.
 
Thanks,
 
Michael
 
 
 
# Function signals various conditions
#
prejudiced <- function(x){
 
    cl <- call("prejudiced",x)

    # throw various conditions with message and information about function call
    if(x==0) signalCondition( simpleCondition("x=0 encountered",call=cl) )
    if(x==1) signalCondition( simpleMessage("Do not like 1",call=cl) )
    if(x==2) signalCondition( simpleWarning("Hate 2",call=cl) )
    if(x==3) signalCondition( simpleError("Cannot tolerate 3",call=cl) )
    return(x)
}
tryCatchTest <- function(x){
    
actual <- tryCatch({
                # expressions evaluated until condition signalled:
                -1
                prejudiced(x)
                4  # returned if no condition is signalled
            },
            # condition handlers return new value as result of tryCatch:
            message = function(m){ print(m); 6},
            warning = function(w){ print(w); 7},
            error   = function(e){ print(e); 8},
            # must be put last otherwise it is called for any condition
            condition = function(c){ print(c); 5},
            finally = { cat("\nCleaned up.") }
    )
    # result of tryCatch:
    expected <- 4
    if(x==0) expected <- 5
    if(x==1) expected <- 6
    if(x==2) expected <- 7
    if(x==3) expected <- 8
    cat("\nResult of tryCatch should be ",expected," and is: ",actual)
}


______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to