> Berry Boessenkool
> on Wed, 1 Mar 2017 14:52:10 + writes:
> Hi Glenn,
> Better late than never:
> couldn't you simply use try?
> result <- try( log("a") )
> The printing is horrible: people will think an error
> occured (but the function didn't stop!)
> I tend to use it like this (which may be totally
> unintended):
> res <- try(log("a"), silent=TRUE)
> if(inherits(res, "try-error"))
> {
> message("log failed: ",res,". Now continuing with res=0.")
> res <- 0
> }
but if you ever looked: try() is just a wrapper to tryCatch()
and using try(*, silent=TRUE) is even closer to a pretty simple tryCatch(.)
Historically, tryCatch() did not exist, but try() did.
So much of "old code" still has try() calls in it.
I consider try() as convenience function for interactive use but
would always use tryCatch() for new code in my packages.
> See here for my version that captures errors/warnings/messages with call
tracing:
> https://www.rdocumentation.org/packages/berryFunctions/topics/tryStack
I'd recommend you switch to tryCatch(): It is more flexible and
more directly "configurable" than try() -- which also got some historical
flexibility by the "hack" (not functional programming)
of depending on getOption("show.error.messages") in the
default case silent = FALSE {needed to suppress other
packages' using try(.) 's printing of error messages as if
*error*s inspite of the fact that they were caught.
and back to the OP:
I think that's your only small problem that you chose
error = function(e) print(e)
because that prints "as if" you had an error.
Martin
> Regards,
> Berry
>
> From: R-package-devel on behalf
of Glenn Schultz
> Sent: Saturday, February 25, 2017 15:50
> To: R Package Development
> Subject: [R-pkg-devel] tryCatch defensive programming guidance
> All,
> I have the following to create a class PriceTypes. I use try catch on
the function and it gives me the error
> price <- tryCatch(PriceTypes(price = "100")
> ,error = function(e) print(e)
> ,warning = function(w) print(w))
>
>>
> I read the section on tryCatch and withCallingHandlers as well the manual
but I am still not clear as to how to use tryCatch in the function below I tried
> PriceTypes <- TryCatch(
> function(){}
> ), function(e) print(error)
> but this is obviously wrong as it did not work. My question can I use
tryCatch in the function itself or only when I invoke the function.
> Best Regards,
> Glenn
> #' An S4 class representating bond price
> #'
> #' This class is used to create and pass the price types reported to
> #' investors and used in analytics. For example price is often reported as
> #' decimal or fractions 32nds to investors but price basis (price/100) is
> #' used to calculate proceeds and compute metrics like yield, duration,
and
> #' partial durations.
> #' @slot PriceDecimal A numeric value the price using decimal notation
> #' @slot Price32nds A character the price using 32nds notation
> #' @slot PriceBasis A numeric value price decimal notation in units of 100
> #' @slot PriceDecimalString A character the price using decimal notation
> #' @exportClass PriceTypes
> setClass("PriceTypes",
> representation(
> PriceDecimal = "numeric",
> Price32nds = "character",
> PriceBasis = "numeric",
> PriceDecimalString = "character")
> )
> setGeneric("PriceTypes", function(price = numeric())
> {standardGeneric("PriceTypes")})
> #' A standard generic function get the slot PriceDecimal
> #'
> #' @param object an S4 object
> #' @export PriceDecimal
> setGeneric("PriceDecimal", function(object)
> {standardGeneric("PriceDecimal")})
> #' A standard generic function to set the slot PriceDecimal
> #'
> #' @param object an S4 object
> #' @param value the replacement value of the slot
> #' @export PriceDecimal<-
> setGeneric("PriceDecimal<-", function(object, value)
> {standardGeneric("PriceDecimal<-")})
> #' A standard generic function to get the slot Price32nds
> #'
> #' @param object an S4 object
> #' @export Price32nds
> setGeneric("Price32nds", function(object)
> {standardGeneric("Price32nds")})
> #' A standard generic function to set the slot Price32nds
> #'
> #' @param object an S4 object
> #' @param value the replacement value of the slot
> #' @export Price32nds<-
> setGeneric("Price32nds<-", function(object, value)
> {setGeneric("Price32nds")})
> #' A standard generic to get the slot PriceBasis
> #'
> #' @param object an S4 object
> #' @export PriceBasis
> setGeneric("PriceBasis", function(object)
> {standardGeneric("PriceBasis")})
> #' A standard generic to set the slot Pric