Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent execution of further code steps after error?

Tags:

r

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?

like image 649
Samo Avatar asked Sep 24 '12 22:09

Samo


People also ask

How do you stop a code after if?

If in case of loops like while, for and foreach, you can use continue; to escape the iteration and break; to end the loop. In function return false; will stop the execution.

How do I stop JavaScript execution at a certain line of code?

Using return to exit a function in javascript Using return is the easiest way to exit a function. You can use return by itself or even return a value.

What syntax stops the execution of line code?

The exit() function only terminates the execution of the script.

Which of the following terms stops the execution of a function?

Which of the following terms stops the execution of a function? Explanation: The return stops the execution of the function when it is encountered within the function.


1 Answers

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.
like image 101
GSee Avatar answered Oct 28 '22 13:10

GSee