开发者

How can I access the name of the function generating an error or warning?

If a warning or error occurs, I would like to print the name of the function generating the error.

tryCatch in R allows one to handle errors in a function call, perhaps it is part of the solution? For 开发者_Python百科example, this could be in a context like:

handleErr <-function(e) {
    print("you had an error in function:")
    print( WHAT CAN I PUT HERE??! )
}

tryCatch(  myFunction(), error=handleErr )


This should work

handleErr <- function(e) 
    {
    cat(paste("you had an error in function: ", e$call, "\n"))
    }


myfunct <- function()
    {
    stop()
    }

tryCatch(myfunct(), error=handleErr)


Look at the traceback function, it shows the call stack for when the last error occured, so you can see what function had the error and also what function called that function, etc.

You can also set options(error= ) to a function to be called when an error occurs, see the help for options to see some functions that already work with this.

You can also set options(warn=2) to promote warnings into errors so that the above tools will work with warnings as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜