Search notes:

R function: tryCatch

tryCatch() can be used catch calls to stop().
divide <- function(a, b) {
  if (b == 0) {
    stop("Don't divide by 0!");
  }
  return(a/b);
}

cause_stop = TRUE

for (z in -2:2) {

  tryCatch (
    {

     if (! cause_stop && z == 0) {
       print('Not going to divide by 0 because cause_stop is FALSE');
       next;
     }
      print(paste('Going to device 5 by', z));
      print(paste('Result is', divide(5, z)));
      print('After division.');
    },
    error = function(cond) {
      print(paste('Error caught:', cond));
      return(NA);
    },
    finally = {
    #
    # Print a new line, regardless wheather division was ok or not.
    #
      cat("\n");
    }
  );
}
Github repository about-R, path: /functions/try/Catch/simple.R

See also

Index to (some) R functions

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759406595, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/R/functions/try/Catch/index(74): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78