5

Possible Duplicate:
Exception handling in R

Does anyone have idea on how to catch an error or an exception in R?

Community
  • 1
  • 1
Shruti
  • 741
  • 4
  • 14
  • 25
  • 6
    http://stackoverflow.com/questions/2622777/exception-handling-in-r – VitoshKa Oct 28 '10 at 21:06
  • 1
    For people like me who got here from Google, this was helpful: http://mazamascience.com/WorkingWithData/?p=912 – Taj Morton Jan 28 '14 at 18:27
  • For those finding this page, this [link](https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r) has a thorough explanation. – steveb Jun 18 '17 at 04:34

3 Answers3

6

Like Joshua said: use tryCatch. Include an error argument, which should be a function accepting one parameter (the error, typically called e).

tryCatch(
  stop("you threw an error"), 
  error = function(e) 
  {
    print(e$message) # or whatever error handling code you want
  }
)
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
2

It really depends on what you mean by "catch". Look at tryCatch and withCallingHandlers.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
1

Have you looked into stop? This will allow you to catch exceptions that you define.

Maiasaura
  • 32,226
  • 27
  • 104
  • 108