Exception                package:R.oo                R Documentation

_T_h_e _E_x_c_e_p_t_i_o_n _c_l_a_s_s _t_o _b_e _t_h_r_o_w_n _a_n_d _c_a_u_g_h_t

_D_e_s_c_r_i_p_t_i_o_n:

     Package:  R.oo 
      *Class Exception*

     'Object'
      '~~|'
      '~~+--''try-error'
      '~~~~~~~|'
      '~~~~~~~+--''condition'
      '~~~~~~~~~~~~|'
      '~~~~~~~~~~~~+--''error'
      '~~~~~~~~~~~~~~~~~|'
      '~~~~~~~~~~~~~~~~~+--''simpleError'
      '~~~~~~~~~~~~~~~~~~~~~~|'
      '~~~~~~~~~~~~~~~~~~~~~~+--''Exception'

     *Directly known subclasses:*
      InternalErrorException, RccViolationException, RdocException

     public static class *Exception*
      extends simpleError

     Creates an Exception that can be thrown and caught. The
     'Exception' class is the root class of all other 'Exception'
     classes.

_U_s_a_g_e:

     Exception(..., sep="", collapse=", ")

_A_r_g_u_m_e_n_t_s:

     ...: One or several strings, which will be concatenated and
          contain informative message about the exception.

     sep: The string to used for concatenating several strings.

collapse: The string to used collapse vectors together.

_F_i_e_l_d_s _a_n_d _M_e_t_h_o_d_s:

     *Methods:*

         'as.character'         Gets a character string representing of the Exception.
         'getCall'              -
         'getLastException'     Static method to get the last Exception thrown.
         'getMessage'           Gets the message of the Exception.
         'getStackTrace'        Gets the stack trace saved when the exception was created.
         'getStackTraceString'  Gets the stack trace as a string.
         'getWhen'              Gets the time when the Exception was created.
         'print'                Prints the Exception.
         'printStackTrace'      Prints the stack trace saved when the exception was created.
         'throw'                Throws an Exception that can be caught.

     *Methods inherited from error*:
      as.character, throw

     *Methods inherited from condition*:
      as.character, conditionCall, conditionMessage, print

     *Methods inherited from Object*:
      $, $<-, [[, [[<-, as.character, attach, attachLocally,
     clearCache, clone, detach, equals, extend, finalize, gc,
     getEnvironment, getFields, getInstanciationTime,
     getStaticInstance, hasField, hashCode, ll, load, objectSize,
     print, save

_A_u_t_h_o_r(_s):

     Henrik Bengtsson (<URL: http://www.braju.com/R/>)

_S_e_e _A_l_s_o:

     See also 'tryCatch'() (and 'try'()).

_E_x_a_m_p_l_e_s:

     ######################################################################
     # 1. To catch a regular "error" exception thrown by e.g. stop().
     ######################################################################
     x <- NA
     y <- NA
     tryCatch({
       x <- log(123);
       y <- log("a");
     }, error = function(ex) {
       print(ex);
     })
     print(x)
     print(y)


     ######################################################################
     # 2. Always run a "final" expression regardless or error or not.
     ######################################################################
     filename <- tempfile("R.methodsS3.example")
     con <- file(filename)
     tryCatch({
       open(con, "r");
     }, error = function(ex) {
       cat("Could not open ", filename, " for reading.\n", sep="")
     }, finally = {
       close(con)
       cat("The id of the connection is ",
            ifelse(is.null(con), "NULL", con), ".\n", sep="")
     })

     ######################################################################
     # 3. Creating your own Exception class
     ######################################################################
     setConstructorS3("NegativeLogValueException", function(
       msg="Trying to calculate the logarithm of a negative value", value=NULL) {
       extend(Exception(msg=msg), "NegativeLogValueException",
         .value = value
       )
     })

     setMethodS3("as.character", "NegativeLogValueException", function(this, ...) {
       paste(as.character.Exception(this), ": ", getValue(this), sep="");
     })

     setMethodS3("getValue", "NegativeLogValueException", function(this, ...) {
       this$.value;
     })

     mylog <- function(x, base=exp(1)) {
       if (x < 0)
         throw(NegativeLogValueException(value=x))
       else
         log(x, base=base)
     }

     # Note that the order of the catch list is important:
     l <- NA;
     x <- 123;
     tryCatch({
       l <- mylog(x);
     }, NegativeLogValueException = function(ex) {
       cat(as.character(ex), "\n")
     }, "try-error" = function(ex) {
       cat("try-error: Could not calculate the logarithm of ", x, ".\n", sep="")
     }, error = function(ex) {
       cat("error: Could not calculate the logarithm of ", x, ".\n", sep="")
     })
     cat("The logarithm of ", x, " is ", l, ".\n\n", sep="")

