How to prevent execution of further code steps after error?

前端 未结 1 760
无人共我
无人共我 2021-01-13 03:04

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 so

相关标签:
1条回答
  • 2021-01-13 03:36

    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.
    
    0 讨论(0)
提交回复
热议问题