6

How can I make sure that after I "catch" an error and log it no further code steps are executed (I do not want to use q())?

My usage scenario is like this: - do some calculations - if error occurs log it - stop executing any further steps in the code

I tried to solve this using code example below (print is used instead of true logging function):

handleMySimpleError<-function(e, text) {
    # Let's log the error
    print(paste0(text, ": ", e))
    # This should stop execution of any further steps but it doesn't
    stop("Now, stop. For real.")
}

print("Starting execution...")
tryCatch(
    stop("My simple error."),
    error=function(e) {handleMySimpleError(e, "could not finish due to")}, finnaly=NULL
)
print("Successfully ended execution...")

I somehow hoped that print("Successfully ended execution...") would never get executed... But, here is the output I get:

> handleMySimpleError<-function(e, text) {
+   # Let's log the error
+   print(paste0(text, ": ", e))
+   # This should stop execution of any further steps but it doesn't
+   stop("Now, stop. For real.")
+ }
>  
> print("Starting execution...")
[1] "Starting execution..."
> tryCatch(
+   stop("My simple error."),
+   error=function(e) {handleMySimpleError(e, "could not finish due to")}, finnaly=NULL
+ )
[1] "could not finish due to: Error in doTryCatch(return(expr), name, parentenv, handler): My simple error.\n"
Error in handleMySimpleError(e, "could not finish due to") : 
Now, stop. For real.
> print("Successfully ended execution...")
[1] "Successfully ended execution..."

How to prevent from print("Successfully ended execution...") being executed? What is the correct strategy to stop code processing after error is logged in error handler function?

Samo
  • 2,065
  • 20
  • 41
  • How are you executing that code? Sourcing a script? – joran Sep 24 '12 at 22:18
  • I am testing it now via StatEt Eclipse plugin using Ctrl+R+R. I would like to use "script" in "production" like: /usr/bin/R --vanilla --quiet < /home/code/MyScript.R – Samo Sep 24 '12 at 22:22
  • When I put that code in a script and source it, it seems to work as you want it to. If I send it directly to the console, I get the behavior you describe in your question. – joran Sep 24 '12 at 22:23
  • joran thank you. Do you maybe know for a work around or better/more suitable strategy to achieve what I want (that would work in all execution scenarios: script and console)? – Samo Sep 24 '12 at 22:30
  • I suspect that the better strategy is to not attempt this by sending code directly to the console. Sourcing scripts is generally better for production code anyway. – joran Sep 24 '12 at 22:32
  • You could wrap all the code in a function, then call that function. – flodel Sep 24 '12 at 23:21

1 Answers1

6

Just wrap curly braces around it

>     {
+       handleMySimpleError<-function(e, text) {
+           # Let's log the error
+           print(paste0(text, ": ", e))
+           # This should stop execution of any further steps but it doesn't
+           stop("Now, stop. For real.")
+       }
+       print("Starting execution...")
+       tryCatch(
+           stop("My simple error."),
+           error=function(e) {handleMySimpleError(e, "could not finish due to")}, finally=NULL
+       )
+       print("Successfully ended execution...") 
+     }
[1] "Starting execution..."
[1] "could not finish due to: Error in doTryCatch(return(expr), name, parentenv, handler): My simple error.\n"
Error in handleMySimpleError(e, "could not finish due to") : 
  Now, stop. For real.
GSee
  • 48,880
  • 13
  • 125
  • 145
  • Thanks. Great. I just need to figure out why using curly braces actually works and why not without them... :) This is counter-intuitive for me. Will consult Pat Burns R Inferno. – Samo Sep 25 '12 at 05:55
  • The problem is that you are executing 1 line at a time. So, after the `stop`, you get an error message, then you execute the next line. If you wrap in braces, you're essentially telling **R**, "run this entire block at once instead of line-by-line." Now, when it gets an error, it exits the code block. – GSee Sep 25 '12 at 12:46
  • I see it now. Thank you. It makes more sense. I assumed the processing is done in similar fashion as in SAS... – Samo Sep 25 '12 at 20:30